sym_glue.c 53 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Device driver for the SYMBIOS/LSILOGIC 53C8XX and 53C1010 family
  4. * of PCI-SCSI IO processors.
  5. *
  6. * Copyright (C) 1999-2001 Gerard Roudier <groudier@free.fr>
  7. * Copyright (c) 2003-2005 Matthew Wilcox <matthew@wil.cx>
  8. *
  9. * This driver is derived from the Linux sym53c8xx driver.
  10. * Copyright (C) 1998-2000 Gerard Roudier
  11. *
  12. * The sym53c8xx driver is derived from the ncr53c8xx driver that had been
  13. * a port of the FreeBSD ncr driver to Linux-1.2.13.
  14. *
  15. * The original ncr driver has been written for 386bsd and FreeBSD by
  16. * Wolfgang Stanglmeier <wolf@cologne.de>
  17. * Stefan Esser <se@mi.Uni-Koeln.de>
  18. * Copyright (C) 1994 Wolfgang Stanglmeier
  19. *
  20. * Other major contributions:
  21. *
  22. * NVRAM detection and reading.
  23. * Copyright (C) 1997 Richard Waltham <dormouse@farsrobt.demon.co.uk>
  24. *
  25. *-----------------------------------------------------------------------------
  26. */
  27. #include <linux/ctype.h>
  28. #include <linux/init.h>
  29. #include <linux/module.h>
  30. #include <linux/moduleparam.h>
  31. #include <linux/spinlock.h>
  32. #include <scsi/scsi.h>
  33. #include <scsi/scsi_tcq.h>
  34. #include <scsi/scsi_device.h>
  35. #include <scsi/scsi_transport.h>
  36. #include "sym_glue.h"
  37. #include "sym_nvram.h"
  38. #define NAME53C "sym53c"
  39. #define NAME53C8XX "sym53c8xx"
  40. struct sym_driver_setup sym_driver_setup = SYM_LINUX_DRIVER_SETUP;
  41. unsigned int sym_debug_flags = 0;
  42. static char *excl_string;
  43. static char *safe_string;
  44. module_param_named(cmd_per_lun, sym_driver_setup.max_tag, ushort, 0);
  45. module_param_named(burst, sym_driver_setup.burst_order, byte, 0);
  46. module_param_named(led, sym_driver_setup.scsi_led, byte, 0);
  47. module_param_named(diff, sym_driver_setup.scsi_diff, byte, 0);
  48. module_param_named(irqm, sym_driver_setup.irq_mode, byte, 0);
  49. module_param_named(buschk, sym_driver_setup.scsi_bus_check, byte, 0);
  50. module_param_named(hostid, sym_driver_setup.host_id, byte, 0);
  51. module_param_named(verb, sym_driver_setup.verbose, byte, 0);
  52. module_param_named(debug, sym_debug_flags, uint, 0);
  53. module_param_named(settle, sym_driver_setup.settle_delay, byte, 0);
  54. module_param_named(nvram, sym_driver_setup.use_nvram, byte, 0);
  55. module_param_named(excl, excl_string, charp, 0);
  56. module_param_named(safe, safe_string, charp, 0);
  57. MODULE_PARM_DESC(cmd_per_lun, "The maximum number of tags to use by default");
  58. MODULE_PARM_DESC(burst, "Maximum burst. 0 to disable, 255 to read from registers");
  59. MODULE_PARM_DESC(led, "Set to 1 to enable LED support");
  60. MODULE_PARM_DESC(diff, "0 for no differential mode, 1 for BIOS, 2 for always, 3 for not GPIO3");
  61. MODULE_PARM_DESC(irqm, "0 for open drain, 1 to leave alone, 2 for totem pole");
  62. MODULE_PARM_DESC(buschk, "0 to not check, 1 for detach on error, 2 for warn on error");
  63. MODULE_PARM_DESC(hostid, "The SCSI ID to use for the host adapters");
  64. MODULE_PARM_DESC(verb, "0 for minimal verbosity, 1 for normal, 2 for excessive");
  65. MODULE_PARM_DESC(debug, "Set bits to enable debugging");
  66. MODULE_PARM_DESC(settle, "Settle delay in seconds. Default 3");
  67. MODULE_PARM_DESC(nvram, "Option currently not used");
  68. MODULE_PARM_DESC(excl, "List ioport addresses here to prevent controllers from being attached");
  69. MODULE_PARM_DESC(safe, "Set other settings to a \"safe mode\"");
  70. MODULE_LICENSE("GPL");
  71. MODULE_VERSION(SYM_VERSION);
  72. MODULE_AUTHOR("Matthew Wilcox <matthew@wil.cx>");
  73. MODULE_DESCRIPTION("NCR, Symbios and LSI 8xx and 1010 PCI SCSI adapters");
  74. static void sym2_setup_params(void)
  75. {
  76. char *p = excl_string;
  77. int xi = 0;
  78. while (p && (xi < 8)) {
  79. char *next_p;
  80. int val = (int) simple_strtoul(p, &next_p, 0);
  81. sym_driver_setup.excludes[xi++] = val;
  82. p = next_p;
  83. }
  84. if (safe_string) {
  85. if (*safe_string == 'y') {
  86. sym_driver_setup.max_tag = 0;
  87. sym_driver_setup.burst_order = 0;
  88. sym_driver_setup.scsi_led = 0;
  89. sym_driver_setup.scsi_diff = 1;
  90. sym_driver_setup.irq_mode = 0;
  91. sym_driver_setup.scsi_bus_check = 2;
  92. sym_driver_setup.host_id = 7;
  93. sym_driver_setup.verbose = 2;
  94. sym_driver_setup.settle_delay = 10;
  95. sym_driver_setup.use_nvram = 1;
  96. } else if (*safe_string != 'n') {
  97. printk(KERN_WARNING NAME53C8XX "Ignoring parameter %s"
  98. " passed to safe option", safe_string);
  99. }
  100. }
  101. }
  102. static struct scsi_transport_template *sym2_transport_template = NULL;
  103. /*
  104. * Driver private area in the SCSI command structure.
  105. */
  106. struct sym_ucmd { /* Override the SCSI pointer structure */
  107. struct completion *eh_done; /* SCSI error handling */
  108. };
  109. #define SYM_UCMD_PTR(cmd) ((struct sym_ucmd *)scsi_cmd_priv(cmd))
  110. #define SYM_SOFTC_PTR(cmd) sym_get_hcb(cmd->device->host)
  111. /*
  112. * Complete a pending CAM CCB.
  113. */
  114. void sym_xpt_done(struct sym_hcb *np, struct scsi_cmnd *cmd)
  115. {
  116. struct sym_ucmd *ucmd = SYM_UCMD_PTR(cmd);
  117. if (ucmd->eh_done)
  118. complete(ucmd->eh_done);
  119. scsi_dma_unmap(cmd);
  120. scsi_done(cmd);
  121. }
  122. /*
  123. * Tell the SCSI layer about a BUS RESET.
  124. */
  125. void sym_xpt_async_bus_reset(struct sym_hcb *np)
  126. {
  127. printf_notice("%s: SCSI BUS has been reset.\n", sym_name(np));
  128. np->s.settle_time = jiffies + sym_driver_setup.settle_delay * HZ;
  129. np->s.settle_time_valid = 1;
  130. if (sym_verbose >= 2)
  131. printf_info("%s: command processing suspended for %d seconds\n",
  132. sym_name(np), sym_driver_setup.settle_delay);
  133. }
  134. /*
  135. * Choose the more appropriate CAM status if
  136. * the IO encountered an extended error.
  137. */
  138. static int sym_xerr_cam_status(int cam_status, int x_status)
  139. {
  140. if (x_status) {
  141. if (x_status & XE_PARITY_ERR)
  142. cam_status = DID_PARITY;
  143. else
  144. cam_status = DID_ERROR;
  145. }
  146. return cam_status;
  147. }
  148. /*
  149. * Build CAM result for a failed or auto-sensed IO.
  150. */
  151. void sym_set_cam_result_error(struct sym_hcb *np, struct sym_ccb *cp, int resid)
  152. {
  153. struct scsi_cmnd *cmd = cp->cmd;
  154. u_int cam_status, scsi_status;
  155. cam_status = DID_OK;
  156. scsi_status = cp->ssss_status;
  157. if (cp->host_flags & HF_SENSE) {
  158. scsi_status = cp->sv_scsi_status;
  159. resid = cp->sv_resid;
  160. if (sym_verbose && cp->sv_xerr_status)
  161. sym_print_xerr(cmd, cp->sv_xerr_status);
  162. if (cp->host_status == HS_COMPLETE &&
  163. cp->ssss_status == S_GOOD &&
  164. cp->xerr_status == 0) {
  165. cam_status = sym_xerr_cam_status(DID_OK,
  166. cp->sv_xerr_status);
  167. /*
  168. * Bounce back the sense data to user.
  169. */
  170. memset(cmd->sense_buffer, 0, SCSI_SENSE_BUFFERSIZE);
  171. memcpy(cmd->sense_buffer, cp->sns_bbuf,
  172. min(SCSI_SENSE_BUFFERSIZE, SYM_SNS_BBUF_LEN));
  173. #if 0
  174. /*
  175. * If the device reports a UNIT ATTENTION condition
  176. * due to a RESET condition, we should consider all
  177. * disconnect CCBs for this unit as aborted.
  178. */
  179. if (1) {
  180. u_char *p;
  181. p = (u_char *) cmd->sense_data;
  182. if (p[0]==0x70 && p[2]==0x6 && p[12]==0x29)
  183. sym_clear_tasks(np, DID_ABORT,
  184. cp->target,cp->lun, -1);
  185. }
  186. #endif
  187. } else {
  188. /*
  189. * Error return from our internal request sense. This
  190. * is bad: we must clear the contingent allegiance
  191. * condition otherwise the device will always return
  192. * BUSY. Use a big stick.
  193. */
  194. sym_reset_scsi_target(np, cmd->device->id);
  195. cam_status = DID_ERROR;
  196. }
  197. } else if (cp->host_status == HS_COMPLETE) /* Bad SCSI status */
  198. cam_status = DID_OK;
  199. else if (cp->host_status == HS_SEL_TIMEOUT) /* Selection timeout */
  200. cam_status = DID_NO_CONNECT;
  201. else if (cp->host_status == HS_UNEXPECTED) /* Unexpected BUS FREE*/
  202. cam_status = DID_ERROR;
  203. else { /* Extended error */
  204. if (sym_verbose) {
  205. sym_print_addr(cmd, "COMMAND FAILED (%x %x %x).\n",
  206. cp->host_status, cp->ssss_status,
  207. cp->xerr_status);
  208. }
  209. /*
  210. * Set the most appropriate value for CAM status.
  211. */
  212. cam_status = sym_xerr_cam_status(DID_ERROR, cp->xerr_status);
  213. }
  214. scsi_set_resid(cmd, resid);
  215. cmd->result = (cam_status << 16) | scsi_status;
  216. }
  217. static int sym_scatter(struct sym_hcb *np, struct sym_ccb *cp, struct scsi_cmnd *cmd)
  218. {
  219. int segment;
  220. int use_sg;
  221. cp->data_len = 0;
  222. use_sg = scsi_dma_map(cmd);
  223. if (use_sg > 0) {
  224. struct scatterlist *sg;
  225. struct sym_tcb *tp = &np->target[cp->target];
  226. struct sym_tblmove *data;
  227. if (use_sg > SYM_CONF_MAX_SG) {
  228. scsi_dma_unmap(cmd);
  229. return -1;
  230. }
  231. data = &cp->phys.data[SYM_CONF_MAX_SG - use_sg];
  232. scsi_for_each_sg(cmd, sg, use_sg, segment) {
  233. dma_addr_t baddr = sg_dma_address(sg);
  234. unsigned int len = sg_dma_len(sg);
  235. if ((len & 1) && (tp->head.wval & EWS)) {
  236. len++;
  237. cp->odd_byte_adjustment++;
  238. }
  239. sym_build_sge(np, &data[segment], baddr, len);
  240. cp->data_len += len;
  241. }
  242. } else {
  243. segment = -2;
  244. }
  245. return segment;
  246. }
  247. /*
  248. * Queue a SCSI command.
  249. */
  250. static int sym_queue_command(struct sym_hcb *np, struct scsi_cmnd *cmd)
  251. {
  252. struct scsi_device *sdev = cmd->device;
  253. struct sym_tcb *tp;
  254. struct sym_lcb *lp;
  255. struct sym_ccb *cp;
  256. int order;
  257. /*
  258. * Retrieve the target descriptor.
  259. */
  260. tp = &np->target[sdev->id];
  261. /*
  262. * Select tagged/untagged.
  263. */
  264. lp = sym_lp(tp, sdev->lun);
  265. order = (lp && lp->s.reqtags) ? M_SIMPLE_TAG : 0;
  266. /*
  267. * Queue the SCSI IO.
  268. */
  269. cp = sym_get_ccb(np, cmd, order);
  270. if (!cp)
  271. return 1; /* Means resource shortage */
  272. sym_queue_scsiio(np, cmd, cp);
  273. return 0;
  274. }
  275. /*
  276. * Setup buffers and pointers that address the CDB.
  277. */
  278. static inline int sym_setup_cdb(struct sym_hcb *np, struct scsi_cmnd *cmd, struct sym_ccb *cp)
  279. {
  280. memcpy(cp->cdb_buf, cmd->cmnd, cmd->cmd_len);
  281. cp->phys.cmd.addr = CCB_BA(cp, cdb_buf[0]);
  282. cp->phys.cmd.size = cpu_to_scr(cmd->cmd_len);
  283. return 0;
  284. }
  285. /*
  286. * Setup pointers that address the data and start the I/O.
  287. */
  288. int sym_setup_data_and_start(struct sym_hcb *np, struct scsi_cmnd *cmd, struct sym_ccb *cp)
  289. {
  290. u32 lastp, goalp;
  291. int dir;
  292. /*
  293. * Build the CDB.
  294. */
  295. if (sym_setup_cdb(np, cmd, cp))
  296. goto out_abort;
  297. /*
  298. * No direction means no data.
  299. */
  300. dir = cmd->sc_data_direction;
  301. if (dir != DMA_NONE) {
  302. cp->segments = sym_scatter(np, cp, cmd);
  303. if (cp->segments < 0) {
  304. sym_set_cam_status(cmd, DID_ERROR);
  305. goto out_abort;
  306. }
  307. /*
  308. * No segments means no data.
  309. */
  310. if (!cp->segments)
  311. dir = DMA_NONE;
  312. } else {
  313. cp->data_len = 0;
  314. cp->segments = 0;
  315. }
  316. /*
  317. * Set the data pointer.
  318. */
  319. switch (dir) {
  320. case DMA_BIDIRECTIONAL:
  321. scmd_printk(KERN_INFO, cmd, "got DMA_BIDIRECTIONAL command");
  322. sym_set_cam_status(cmd, DID_ERROR);
  323. goto out_abort;
  324. case DMA_TO_DEVICE:
  325. goalp = SCRIPTA_BA(np, data_out2) + 8;
  326. lastp = goalp - 8 - (cp->segments * (2*4));
  327. break;
  328. case DMA_FROM_DEVICE:
  329. cp->host_flags |= HF_DATA_IN;
  330. goalp = SCRIPTA_BA(np, data_in2) + 8;
  331. lastp = goalp - 8 - (cp->segments * (2*4));
  332. break;
  333. case DMA_NONE:
  334. default:
  335. lastp = goalp = SCRIPTB_BA(np, no_data);
  336. break;
  337. }
  338. /*
  339. * Set all pointers values needed by SCRIPTS.
  340. */
  341. cp->phys.head.lastp = cpu_to_scr(lastp);
  342. cp->phys.head.savep = cpu_to_scr(lastp);
  343. cp->startp = cp->phys.head.savep;
  344. cp->goalp = cpu_to_scr(goalp);
  345. /*
  346. * When `#ifed 1', the code below makes the driver
  347. * panic on the first attempt to write to a SCSI device.
  348. * It is the first test we want to do after a driver
  349. * change that does not seem obviously safe. :)
  350. */
  351. #if 0
  352. switch (cp->cdb_buf[0]) {
  353. case 0x0A: case 0x2A: case 0xAA:
  354. panic("XXXXXXXXXXXXX WRITE NOT YET ALLOWED XXXXXXXXXXXXXX\n");
  355. break;
  356. default:
  357. break;
  358. }
  359. #endif
  360. /*
  361. * activate this job.
  362. */
  363. sym_put_start_queue(np, cp);
  364. return 0;
  365. out_abort:
  366. sym_free_ccb(np, cp);
  367. sym_xpt_done(np, cmd);
  368. return 0;
  369. }
  370. /*
  371. * timer daemon.
  372. *
  373. * Misused to keep the driver running when
  374. * interrupts are not configured correctly.
  375. */
  376. static void sym_timer(struct sym_hcb *np)
  377. {
  378. unsigned long thistime = jiffies;
  379. /*
  380. * Restart the timer.
  381. */
  382. np->s.timer.expires = thistime + SYM_CONF_TIMER_INTERVAL;
  383. add_timer(&np->s.timer);
  384. /*
  385. * If we are resetting the ncr, wait for settle_time before
  386. * clearing it. Then command processing will be resumed.
  387. */
  388. if (np->s.settle_time_valid) {
  389. if (time_before_eq(np->s.settle_time, thistime)) {
  390. if (sym_verbose >= 2 )
  391. printk("%s: command processing resumed\n",
  392. sym_name(np));
  393. np->s.settle_time_valid = 0;
  394. }
  395. return;
  396. }
  397. /*
  398. * Nothing to do for now, but that may come.
  399. */
  400. if (np->s.lasttime + 4*HZ < thistime) {
  401. np->s.lasttime = thistime;
  402. }
  403. #ifdef SYM_CONF_PCIQ_MAY_MISS_COMPLETIONS
  404. /*
  405. * Some way-broken PCI bridges may lead to
  406. * completions being lost when the clearing
  407. * of the INTFLY flag by the CPU occurs
  408. * concurrently with the chip raising this flag.
  409. * If this ever happen, lost completions will
  410. * be reaped here.
  411. */
  412. sym_wakeup_done(np);
  413. #endif
  414. }
  415. /*
  416. * PCI BUS error handler.
  417. */
  418. void sym_log_bus_error(struct Scsi_Host *shost)
  419. {
  420. struct sym_data *sym_data = shost_priv(shost);
  421. struct pci_dev *pdev = sym_data->pdev;
  422. unsigned short pci_sts;
  423. pci_read_config_word(pdev, PCI_STATUS, &pci_sts);
  424. if (pci_sts & 0xf900) {
  425. pci_write_config_word(pdev, PCI_STATUS, pci_sts);
  426. shost_printk(KERN_WARNING, shost,
  427. "PCI bus error: status = 0x%04x\n", pci_sts & 0xf900);
  428. }
  429. }
  430. /*
  431. * queuecommand method. Entered with the host adapter lock held and
  432. * interrupts disabled.
  433. */
  434. static enum scsi_qc_status sym53c8xx_queue_command_lck(struct scsi_cmnd *cmd)
  435. {
  436. struct sym_hcb *np = SYM_SOFTC_PTR(cmd);
  437. struct sym_ucmd *ucp = SYM_UCMD_PTR(cmd);
  438. int sts = 0;
  439. memset(ucp, 0, sizeof(*ucp));
  440. /*
  441. * Shorten our settle_time if needed for
  442. * this command not to time out.
  443. */
  444. if (np->s.settle_time_valid && scsi_cmd_to_rq(cmd)->timeout) {
  445. unsigned long tlimit = jiffies + scsi_cmd_to_rq(cmd)->timeout;
  446. tlimit -= SYM_CONF_TIMER_INTERVAL*2;
  447. if (time_after(np->s.settle_time, tlimit)) {
  448. np->s.settle_time = tlimit;
  449. }
  450. }
  451. if (np->s.settle_time_valid)
  452. return SCSI_MLQUEUE_HOST_BUSY;
  453. sts = sym_queue_command(np, cmd);
  454. if (sts)
  455. return SCSI_MLQUEUE_HOST_BUSY;
  456. return 0;
  457. }
  458. static DEF_SCSI_QCMD(sym53c8xx_queue_command)
  459. /*
  460. * Linux entry point of the interrupt handler.
  461. */
  462. static irqreturn_t sym53c8xx_intr(int irq, void *dev_id)
  463. {
  464. struct Scsi_Host *shost = dev_id;
  465. struct sym_data *sym_data = shost_priv(shost);
  466. irqreturn_t result;
  467. /* Avoid spinloop trying to handle interrupts on frozen device */
  468. if (pci_channel_offline(sym_data->pdev))
  469. return IRQ_NONE;
  470. if (DEBUG_FLAGS & DEBUG_TINY) printf_debug ("[");
  471. spin_lock(shost->host_lock);
  472. result = sym_interrupt(shost);
  473. spin_unlock(shost->host_lock);
  474. if (DEBUG_FLAGS & DEBUG_TINY) printf_debug ("]\n");
  475. return result;
  476. }
  477. /*
  478. * Linux entry point of the timer handler
  479. */
  480. static void sym53c8xx_timer(struct timer_list *t)
  481. {
  482. struct sym_hcb *np = timer_container_of(np, t, s.timer);
  483. unsigned long flags;
  484. spin_lock_irqsave(np->s.host->host_lock, flags);
  485. sym_timer(np);
  486. spin_unlock_irqrestore(np->s.host->host_lock, flags);
  487. }
  488. /*
  489. * What the eh thread wants us to perform.
  490. */
  491. #define SYM_EH_ABORT 0
  492. #define SYM_EH_DEVICE_RESET 1
  493. /*
  494. * Generic method for our eh processing.
  495. * The 'op' argument tells what we have to do.
  496. */
  497. /*
  498. * Error handlers called from the eh thread (one thread per HBA).
  499. */
  500. static int sym53c8xx_eh_abort_handler(struct scsi_cmnd *cmd)
  501. {
  502. struct sym_ucmd *ucmd = SYM_UCMD_PTR(cmd);
  503. struct Scsi_Host *shost = cmd->device->host;
  504. struct sym_data *sym_data = shost_priv(shost);
  505. struct pci_dev *pdev = sym_data->pdev;
  506. struct sym_hcb *np = sym_data->ncb;
  507. SYM_QUEHEAD *qp;
  508. int cmd_queued = 0;
  509. int sts = -1;
  510. struct completion eh_done;
  511. scmd_printk(KERN_WARNING, cmd, "ABORT operation started\n");
  512. /*
  513. * Escalate to host reset if the PCI bus went down
  514. */
  515. if (pci_channel_offline(pdev))
  516. return SCSI_FAILED;
  517. spin_lock_irq(shost->host_lock);
  518. /* This one is queued in some place -> to wait for completion */
  519. FOR_EACH_QUEUED_ELEMENT(&np->busy_ccbq, qp) {
  520. struct sym_ccb *cp = sym_que_entry(qp, struct sym_ccb, link_ccbq);
  521. if (cp->cmd == cmd) {
  522. cmd_queued = 1;
  523. break;
  524. }
  525. }
  526. sts = sym_abort_scsiio(np, cmd, 1);
  527. /* On error, restore everything and cross fingers :) */
  528. if (sts)
  529. cmd_queued = 0;
  530. if (cmd_queued) {
  531. init_completion(&eh_done);
  532. ucmd->eh_done = &eh_done;
  533. spin_unlock_irq(shost->host_lock);
  534. if (!wait_for_completion_timeout(&eh_done, 5*HZ)) {
  535. ucmd->eh_done = NULL;
  536. sts = -2;
  537. }
  538. } else {
  539. spin_unlock_irq(shost->host_lock);
  540. }
  541. dev_warn(&cmd->device->sdev_gendev, "ABORT operation %s.\n",
  542. sts==0 ? "complete" :sts==-2 ? "timed-out" : "failed");
  543. return sts ? SCSI_FAILED : SCSI_SUCCESS;
  544. }
  545. static int sym53c8xx_eh_target_reset_handler(struct scsi_cmnd *cmd)
  546. {
  547. struct scsi_target *starget = scsi_target(cmd->device);
  548. struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
  549. struct sym_data *sym_data = shost_priv(shost);
  550. struct pci_dev *pdev = sym_data->pdev;
  551. struct sym_hcb *np = sym_data->ncb;
  552. SYM_QUEHEAD *qp;
  553. int sts;
  554. struct completion eh_done;
  555. starget_printk(KERN_WARNING, starget,
  556. "TARGET RESET operation started\n");
  557. /*
  558. * Escalate to host reset if the PCI bus went down
  559. */
  560. if (pci_channel_offline(pdev))
  561. return SCSI_FAILED;
  562. spin_lock_irq(shost->host_lock);
  563. sts = sym_reset_scsi_target(np, starget->id);
  564. if (!sts) {
  565. FOR_EACH_QUEUED_ELEMENT(&np->busy_ccbq, qp) {
  566. struct sym_ccb *cp = sym_que_entry(qp, struct sym_ccb,
  567. link_ccbq);
  568. struct scsi_cmnd *cmd = cp->cmd;
  569. struct sym_ucmd *ucmd;
  570. if (!cmd || cmd->device->channel != starget->channel ||
  571. cmd->device->id != starget->id)
  572. continue;
  573. ucmd = SYM_UCMD_PTR(cmd);
  574. init_completion(&eh_done);
  575. ucmd->eh_done = &eh_done;
  576. spin_unlock_irq(shost->host_lock);
  577. if (!wait_for_completion_timeout(&eh_done, 5*HZ)) {
  578. ucmd->eh_done = NULL;
  579. sts = -2;
  580. }
  581. spin_lock_irq(shost->host_lock);
  582. }
  583. }
  584. spin_unlock_irq(shost->host_lock);
  585. starget_printk(KERN_WARNING, starget, "TARGET RESET operation %s.\n",
  586. sts==0 ? "complete" :sts==-2 ? "timed-out" : "failed");
  587. return SCSI_SUCCESS;
  588. }
  589. static int sym53c8xx_eh_bus_reset_handler(struct scsi_cmnd *cmd)
  590. {
  591. struct Scsi_Host *shost = cmd->device->host;
  592. struct sym_data *sym_data = shost_priv(shost);
  593. struct pci_dev *pdev = sym_data->pdev;
  594. struct sym_hcb *np = sym_data->ncb;
  595. scmd_printk(KERN_WARNING, cmd, "BUS RESET operation started\n");
  596. /*
  597. * Escalate to host reset if the PCI bus went down
  598. */
  599. if (pci_channel_offline(pdev))
  600. return SCSI_FAILED;
  601. spin_lock_irq(shost->host_lock);
  602. sym_reset_scsi_bus(np, 1);
  603. spin_unlock_irq(shost->host_lock);
  604. dev_warn(&cmd->device->sdev_gendev, "BUS RESET operation complete.\n");
  605. return SCSI_SUCCESS;
  606. }
  607. static int sym53c8xx_eh_host_reset_handler(struct scsi_cmnd *cmd)
  608. {
  609. struct Scsi_Host *shost = cmd->device->host;
  610. struct sym_data *sym_data = shost_priv(shost);
  611. struct pci_dev *pdev = sym_data->pdev;
  612. struct sym_hcb *np = sym_data->ncb;
  613. struct completion eh_done;
  614. int finished_reset = 1;
  615. shost_printk(KERN_WARNING, shost, "HOST RESET operation started\n");
  616. /* We may be in an error condition because the PCI bus
  617. * went down. In this case, we need to wait until the
  618. * PCI bus is reset, the card is reset, and only then
  619. * proceed with the scsi error recovery. There's no
  620. * point in hurrying; take a leisurely wait.
  621. */
  622. #define WAIT_FOR_PCI_RECOVERY 35
  623. if (pci_channel_offline(pdev)) {
  624. init_completion(&eh_done);
  625. spin_lock_irq(shost->host_lock);
  626. /* Make sure we didn't race */
  627. if (pci_channel_offline(pdev)) {
  628. BUG_ON(sym_data->io_reset);
  629. sym_data->io_reset = &eh_done;
  630. finished_reset = 0;
  631. }
  632. spin_unlock_irq(shost->host_lock);
  633. if (!finished_reset)
  634. finished_reset = wait_for_completion_timeout
  635. (sym_data->io_reset,
  636. WAIT_FOR_PCI_RECOVERY*HZ);
  637. spin_lock_irq(shost->host_lock);
  638. sym_data->io_reset = NULL;
  639. spin_unlock_irq(shost->host_lock);
  640. }
  641. if (finished_reset) {
  642. sym_reset_scsi_bus(np, 0);
  643. sym_start_up(shost, 1);
  644. }
  645. shost_printk(KERN_WARNING, shost, "HOST RESET operation %s.\n",
  646. finished_reset==1 ? "complete" : "failed");
  647. return finished_reset ? SCSI_SUCCESS : SCSI_FAILED;
  648. }
  649. /*
  650. * Tune device queuing depth, according to various limits.
  651. */
  652. static void sym_tune_dev_queuing(struct sym_tcb *tp, int lun, u_short reqtags)
  653. {
  654. struct sym_lcb *lp = sym_lp(tp, lun);
  655. u_short oldtags;
  656. if (!lp)
  657. return;
  658. oldtags = lp->s.reqtags;
  659. if (reqtags > lp->s.scdev_depth)
  660. reqtags = lp->s.scdev_depth;
  661. lp->s.reqtags = reqtags;
  662. if (reqtags != oldtags) {
  663. dev_info(&tp->starget->dev,
  664. "tagged command queuing %s, command queue depth %d.\n",
  665. lp->s.reqtags ? "enabled" : "disabled", reqtags);
  666. }
  667. }
  668. static int sym53c8xx_sdev_init(struct scsi_device *sdev)
  669. {
  670. struct sym_hcb *np = sym_get_hcb(sdev->host);
  671. struct sym_tcb *tp = &np->target[sdev->id];
  672. struct sym_lcb *lp;
  673. unsigned long flags;
  674. int error;
  675. if (sdev->id >= SYM_CONF_MAX_TARGET || sdev->lun >= SYM_CONF_MAX_LUN)
  676. return -ENXIO;
  677. spin_lock_irqsave(np->s.host->host_lock, flags);
  678. /*
  679. * Fail the device init if the device is flagged NOSCAN at BOOT in
  680. * the NVRAM. This may speed up boot and maintain coherency with
  681. * BIOS device numbering. Clearing the flag allows the user to
  682. * rescan skipped devices later. We also return an error for
  683. * devices not flagged for SCAN LUNS in the NVRAM since some single
  684. * lun devices behave badly when asked for a non zero LUN.
  685. */
  686. if (tp->usrflags & SYM_SCAN_BOOT_DISABLED) {
  687. tp->usrflags &= ~SYM_SCAN_BOOT_DISABLED;
  688. starget_printk(KERN_INFO, sdev->sdev_target,
  689. "Scan at boot disabled in NVRAM\n");
  690. error = -ENXIO;
  691. goto out;
  692. }
  693. if (tp->usrflags & SYM_SCAN_LUNS_DISABLED) {
  694. if (sdev->lun != 0) {
  695. error = -ENXIO;
  696. goto out;
  697. }
  698. starget_printk(KERN_INFO, sdev->sdev_target,
  699. "Multiple LUNs disabled in NVRAM\n");
  700. }
  701. lp = sym_alloc_lcb(np, sdev->id, sdev->lun);
  702. if (!lp) {
  703. error = -ENOMEM;
  704. goto out;
  705. }
  706. if (tp->nlcb == 1)
  707. tp->starget = sdev->sdev_target;
  708. spi_min_period(tp->starget) = tp->usr_period;
  709. spi_max_width(tp->starget) = tp->usr_width;
  710. error = 0;
  711. out:
  712. spin_unlock_irqrestore(np->s.host->host_lock, flags);
  713. return error;
  714. }
  715. /*
  716. * Linux entry point for device queue sizing.
  717. */
  718. static int sym53c8xx_sdev_configure(struct scsi_device *sdev,
  719. struct queue_limits *lim)
  720. {
  721. struct sym_hcb *np = sym_get_hcb(sdev->host);
  722. struct sym_tcb *tp = &np->target[sdev->id];
  723. struct sym_lcb *lp = sym_lp(tp, sdev->lun);
  724. int reqtags, depth_to_use;
  725. /*
  726. * Get user flags.
  727. */
  728. lp->curr_flags = lp->user_flags;
  729. /*
  730. * Select queue depth from driver setup.
  731. * Do not use more than configured by user.
  732. * Use at least 1.
  733. * Do not use more than our maximum.
  734. */
  735. reqtags = sym_driver_setup.max_tag;
  736. if (reqtags > tp->usrtags)
  737. reqtags = tp->usrtags;
  738. if (!sdev->tagged_supported)
  739. reqtags = 0;
  740. if (reqtags > SYM_CONF_MAX_TAG)
  741. reqtags = SYM_CONF_MAX_TAG;
  742. depth_to_use = reqtags ? reqtags : 1;
  743. scsi_change_queue_depth(sdev, depth_to_use);
  744. lp->s.scdev_depth = depth_to_use;
  745. sym_tune_dev_queuing(tp, sdev->lun, reqtags);
  746. if (!spi_initial_dv(sdev->sdev_target))
  747. spi_dv_device(sdev);
  748. return 0;
  749. }
  750. static void sym53c8xx_sdev_destroy(struct scsi_device *sdev)
  751. {
  752. struct sym_hcb *np = sym_get_hcb(sdev->host);
  753. struct sym_tcb *tp = &np->target[sdev->id];
  754. struct sym_lcb *lp = sym_lp(tp, sdev->lun);
  755. unsigned long flags;
  756. /* if sdev_init returned before allocating a sym_lcb, return */
  757. if (!lp)
  758. return;
  759. spin_lock_irqsave(np->s.host->host_lock, flags);
  760. if (lp->busy_itlq || lp->busy_itl) {
  761. /*
  762. * This really shouldn't happen, but we can't return an error
  763. * so let's try to stop all on-going I/O.
  764. */
  765. starget_printk(KERN_WARNING, tp->starget,
  766. "Removing busy LCB (%d)\n", (u8)sdev->lun);
  767. sym_reset_scsi_bus(np, 1);
  768. }
  769. if (sym_free_lcb(np, sdev->id, sdev->lun) == 0) {
  770. /*
  771. * It was the last unit for this target.
  772. */
  773. tp->head.sval = 0;
  774. tp->head.wval = np->rv_scntl3;
  775. tp->head.uval = 0;
  776. tp->tgoal.check_nego = 1;
  777. tp->starget = NULL;
  778. }
  779. spin_unlock_irqrestore(np->s.host->host_lock, flags);
  780. }
  781. /*
  782. * Linux entry point for info() function
  783. */
  784. static const char *sym53c8xx_info (struct Scsi_Host *host)
  785. {
  786. return SYM_DRIVER_NAME;
  787. }
  788. #ifdef SYM_LINUX_PROC_INFO_SUPPORT
  789. /*
  790. * Proc file system stuff
  791. *
  792. * A read operation returns adapter information.
  793. * A write operation is a control command.
  794. * The string is parsed in the driver code and the command is passed
  795. * to the sym_usercmd() function.
  796. */
  797. #ifdef SYM_LINUX_USER_COMMAND_SUPPORT
  798. struct sym_usrcmd {
  799. u_long target;
  800. u_long lun;
  801. u_long data;
  802. u_long cmd;
  803. };
  804. #define UC_SETSYNC 10
  805. #define UC_SETTAGS 11
  806. #define UC_SETDEBUG 12
  807. #define UC_SETWIDE 14
  808. #define UC_SETFLAG 15
  809. #define UC_SETVERBOSE 17
  810. #define UC_RESETDEV 18
  811. #define UC_CLEARDEV 19
  812. static void sym_exec_user_command (struct sym_hcb *np, struct sym_usrcmd *uc)
  813. {
  814. struct sym_tcb *tp;
  815. int t, l;
  816. switch (uc->cmd) {
  817. case 0: return;
  818. #ifdef SYM_LINUX_DEBUG_CONTROL_SUPPORT
  819. case UC_SETDEBUG:
  820. sym_debug_flags = uc->data;
  821. break;
  822. #endif
  823. case UC_SETVERBOSE:
  824. np->verbose = uc->data;
  825. break;
  826. default:
  827. /*
  828. * We assume that other commands apply to targets.
  829. * This should always be the case and avoid the below
  830. * 4 lines to be repeated 6 times.
  831. */
  832. for (t = 0; t < SYM_CONF_MAX_TARGET; t++) {
  833. if (!((uc->target >> t) & 1))
  834. continue;
  835. tp = &np->target[t];
  836. if (!tp->nlcb)
  837. continue;
  838. switch (uc->cmd) {
  839. case UC_SETSYNC:
  840. if (!uc->data || uc->data >= 255) {
  841. tp->tgoal.iu = tp->tgoal.dt =
  842. tp->tgoal.qas = 0;
  843. tp->tgoal.offset = 0;
  844. } else if (uc->data <= 9 && np->minsync_dt) {
  845. if (uc->data < np->minsync_dt)
  846. uc->data = np->minsync_dt;
  847. tp->tgoal.iu = tp->tgoal.dt =
  848. tp->tgoal.qas = 1;
  849. tp->tgoal.width = 1;
  850. tp->tgoal.period = uc->data;
  851. tp->tgoal.offset = np->maxoffs_dt;
  852. } else {
  853. if (uc->data < np->minsync)
  854. uc->data = np->minsync;
  855. tp->tgoal.iu = tp->tgoal.dt =
  856. tp->tgoal.qas = 0;
  857. tp->tgoal.period = uc->data;
  858. tp->tgoal.offset = np->maxoffs;
  859. }
  860. tp->tgoal.check_nego = 1;
  861. break;
  862. case UC_SETWIDE:
  863. tp->tgoal.width = uc->data ? 1 : 0;
  864. tp->tgoal.check_nego = 1;
  865. break;
  866. case UC_SETTAGS:
  867. for (l = 0; l < SYM_CONF_MAX_LUN; l++)
  868. sym_tune_dev_queuing(tp, l, uc->data);
  869. break;
  870. case UC_RESETDEV:
  871. tp->to_reset = 1;
  872. np->istat_sem = SEM;
  873. OUTB(np, nc_istat, SIGP|SEM);
  874. break;
  875. case UC_CLEARDEV:
  876. for (l = 0; l < SYM_CONF_MAX_LUN; l++) {
  877. struct sym_lcb *lp = sym_lp(tp, l);
  878. if (lp) lp->to_clear = 1;
  879. }
  880. np->istat_sem = SEM;
  881. OUTB(np, nc_istat, SIGP|SEM);
  882. break;
  883. case UC_SETFLAG:
  884. tp->usrflags = uc->data;
  885. break;
  886. }
  887. }
  888. break;
  889. }
  890. }
  891. static int sym_skip_spaces(char *ptr, int len)
  892. {
  893. int cnt, c;
  894. for (cnt = len; cnt > 0 && (c = *ptr++) && isspace(c); cnt--);
  895. return (len - cnt);
  896. }
  897. static int get_int_arg(char *ptr, int len, u_long *pv)
  898. {
  899. char *end;
  900. *pv = simple_strtoul(ptr, &end, 10);
  901. return (end - ptr);
  902. }
  903. static int is_keyword(char *ptr, int len, char *verb)
  904. {
  905. int verb_len = strlen(verb);
  906. if (len >= verb_len && !memcmp(verb, ptr, verb_len))
  907. return verb_len;
  908. else
  909. return 0;
  910. }
  911. #define SKIP_SPACES(ptr, len) \
  912. if ((arg_len = sym_skip_spaces(ptr, len)) < 1) \
  913. return -EINVAL; \
  914. ptr += arg_len; len -= arg_len;
  915. #define GET_INT_ARG(ptr, len, v) \
  916. if (!(arg_len = get_int_arg(ptr, len, &(v)))) \
  917. return -EINVAL; \
  918. ptr += arg_len; len -= arg_len;
  919. /*
  920. * Parse a control command
  921. */
  922. static int sym_user_command(struct Scsi_Host *shost, char *buffer, int length)
  923. {
  924. struct sym_hcb *np = sym_get_hcb(shost);
  925. char *ptr = buffer;
  926. int len = length;
  927. struct sym_usrcmd cmd, *uc = &cmd;
  928. int arg_len;
  929. u_long target;
  930. memset(uc, 0, sizeof(*uc));
  931. if (len > 0 && ptr[len-1] == '\n')
  932. --len;
  933. if ((arg_len = is_keyword(ptr, len, "setsync")) != 0)
  934. uc->cmd = UC_SETSYNC;
  935. else if ((arg_len = is_keyword(ptr, len, "settags")) != 0)
  936. uc->cmd = UC_SETTAGS;
  937. else if ((arg_len = is_keyword(ptr, len, "setverbose")) != 0)
  938. uc->cmd = UC_SETVERBOSE;
  939. else if ((arg_len = is_keyword(ptr, len, "setwide")) != 0)
  940. uc->cmd = UC_SETWIDE;
  941. #ifdef SYM_LINUX_DEBUG_CONTROL_SUPPORT
  942. else if ((arg_len = is_keyword(ptr, len, "setdebug")) != 0)
  943. uc->cmd = UC_SETDEBUG;
  944. #endif
  945. else if ((arg_len = is_keyword(ptr, len, "setflag")) != 0)
  946. uc->cmd = UC_SETFLAG;
  947. else if ((arg_len = is_keyword(ptr, len, "resetdev")) != 0)
  948. uc->cmd = UC_RESETDEV;
  949. else if ((arg_len = is_keyword(ptr, len, "cleardev")) != 0)
  950. uc->cmd = UC_CLEARDEV;
  951. else
  952. arg_len = 0;
  953. #ifdef DEBUG_PROC_INFO
  954. printk("sym_user_command: arg_len=%d, cmd=%ld\n", arg_len, uc->cmd);
  955. #endif
  956. if (!arg_len)
  957. return -EINVAL;
  958. ptr += arg_len; len -= arg_len;
  959. switch(uc->cmd) {
  960. case UC_SETSYNC:
  961. case UC_SETTAGS:
  962. case UC_SETWIDE:
  963. case UC_SETFLAG:
  964. case UC_RESETDEV:
  965. case UC_CLEARDEV:
  966. SKIP_SPACES(ptr, len);
  967. if ((arg_len = is_keyword(ptr, len, "all")) != 0) {
  968. ptr += arg_len; len -= arg_len;
  969. uc->target = ~0;
  970. } else {
  971. GET_INT_ARG(ptr, len, target);
  972. uc->target = (1<<target);
  973. #ifdef DEBUG_PROC_INFO
  974. printk("sym_user_command: target=%ld\n", target);
  975. #endif
  976. }
  977. break;
  978. }
  979. switch(uc->cmd) {
  980. case UC_SETVERBOSE:
  981. case UC_SETSYNC:
  982. case UC_SETTAGS:
  983. case UC_SETWIDE:
  984. SKIP_SPACES(ptr, len);
  985. GET_INT_ARG(ptr, len, uc->data);
  986. #ifdef DEBUG_PROC_INFO
  987. printk("sym_user_command: data=%ld\n", uc->data);
  988. #endif
  989. break;
  990. #ifdef SYM_LINUX_DEBUG_CONTROL_SUPPORT
  991. case UC_SETDEBUG:
  992. while (len > 0) {
  993. SKIP_SPACES(ptr, len);
  994. if ((arg_len = is_keyword(ptr, len, "alloc")))
  995. uc->data |= DEBUG_ALLOC;
  996. else if ((arg_len = is_keyword(ptr, len, "phase")))
  997. uc->data |= DEBUG_PHASE;
  998. else if ((arg_len = is_keyword(ptr, len, "queue")))
  999. uc->data |= DEBUG_QUEUE;
  1000. else if ((arg_len = is_keyword(ptr, len, "result")))
  1001. uc->data |= DEBUG_RESULT;
  1002. else if ((arg_len = is_keyword(ptr, len, "scatter")))
  1003. uc->data |= DEBUG_SCATTER;
  1004. else if ((arg_len = is_keyword(ptr, len, "script")))
  1005. uc->data |= DEBUG_SCRIPT;
  1006. else if ((arg_len = is_keyword(ptr, len, "tiny")))
  1007. uc->data |= DEBUG_TINY;
  1008. else if ((arg_len = is_keyword(ptr, len, "timing")))
  1009. uc->data |= DEBUG_TIMING;
  1010. else if ((arg_len = is_keyword(ptr, len, "nego")))
  1011. uc->data |= DEBUG_NEGO;
  1012. else if ((arg_len = is_keyword(ptr, len, "tags")))
  1013. uc->data |= DEBUG_TAGS;
  1014. else if ((arg_len = is_keyword(ptr, len, "pointer")))
  1015. uc->data |= DEBUG_POINTER;
  1016. else
  1017. return -EINVAL;
  1018. ptr += arg_len; len -= arg_len;
  1019. }
  1020. #ifdef DEBUG_PROC_INFO
  1021. printk("sym_user_command: data=%ld\n", uc->data);
  1022. #endif
  1023. break;
  1024. #endif /* SYM_LINUX_DEBUG_CONTROL_SUPPORT */
  1025. case UC_SETFLAG:
  1026. while (len > 0) {
  1027. SKIP_SPACES(ptr, len);
  1028. if ((arg_len = is_keyword(ptr, len, "no_disc")))
  1029. uc->data &= ~SYM_DISC_ENABLED;
  1030. else
  1031. return -EINVAL;
  1032. ptr += arg_len; len -= arg_len;
  1033. }
  1034. break;
  1035. default:
  1036. break;
  1037. }
  1038. if (len)
  1039. return -EINVAL;
  1040. else {
  1041. unsigned long flags;
  1042. spin_lock_irqsave(shost->host_lock, flags);
  1043. sym_exec_user_command(np, uc);
  1044. spin_unlock_irqrestore(shost->host_lock, flags);
  1045. }
  1046. return length;
  1047. }
  1048. #endif /* SYM_LINUX_USER_COMMAND_SUPPORT */
  1049. /*
  1050. * Copy formatted information into the input buffer.
  1051. */
  1052. static int sym_show_info(struct seq_file *m, struct Scsi_Host *shost)
  1053. {
  1054. #ifdef SYM_LINUX_USER_INFO_SUPPORT
  1055. struct sym_data *sym_data = shost_priv(shost);
  1056. struct pci_dev *pdev = sym_data->pdev;
  1057. struct sym_hcb *np = sym_data->ncb;
  1058. seq_printf(m, "Chip " NAME53C "%s, device id 0x%x, "
  1059. "revision id 0x%x\n", np->s.chip_name,
  1060. pdev->device, pdev->revision);
  1061. seq_printf(m, "At PCI address %s, IRQ %u\n",
  1062. pci_name(pdev), pdev->irq);
  1063. seq_printf(m, "Min. period factor %d, %s SCSI BUS%s\n",
  1064. (int) (np->minsync_dt ? np->minsync_dt : np->minsync),
  1065. np->maxwide ? "Wide" : "Narrow",
  1066. np->minsync_dt ? ", DT capable" : "");
  1067. seq_printf(m, "Max. started commands %d, "
  1068. "max. commands per LUN %d\n",
  1069. SYM_CONF_MAX_START, SYM_CONF_MAX_TAG);
  1070. return 0;
  1071. #else
  1072. return -EINVAL;
  1073. #endif /* SYM_LINUX_USER_INFO_SUPPORT */
  1074. }
  1075. #endif /* SYM_LINUX_PROC_INFO_SUPPORT */
  1076. /*
  1077. * Free resources claimed by sym_iomap_device(). Note that
  1078. * sym_free_resources() should be used instead of this function after calling
  1079. * sym_attach().
  1080. */
  1081. static void sym_iounmap_device(struct sym_device *device)
  1082. {
  1083. if (device->s.ioaddr)
  1084. pci_iounmap(device->pdev, device->s.ioaddr);
  1085. if (device->s.ramaddr)
  1086. pci_iounmap(device->pdev, device->s.ramaddr);
  1087. }
  1088. /*
  1089. * Free controller resources.
  1090. */
  1091. static void sym_free_resources(struct sym_hcb *np, struct pci_dev *pdev,
  1092. int do_free_irq)
  1093. {
  1094. /*
  1095. * Free O/S specific resources.
  1096. */
  1097. if (do_free_irq)
  1098. free_irq(pdev->irq, np->s.host);
  1099. if (np->s.ioaddr)
  1100. pci_iounmap(pdev, np->s.ioaddr);
  1101. if (np->s.ramaddr)
  1102. pci_iounmap(pdev, np->s.ramaddr);
  1103. /*
  1104. * Free O/S independent resources.
  1105. */
  1106. sym_hcb_free(np);
  1107. sym_mfree_dma(np, sizeof(*np), "HCB");
  1108. }
  1109. /*
  1110. * Host attach and initialisations.
  1111. *
  1112. * Allocate host data and ncb structure.
  1113. * Remap MMIO region.
  1114. * Do chip initialization.
  1115. * If all is OK, install interrupt handling and
  1116. * start the timer daemon.
  1117. */
  1118. static struct Scsi_Host *sym_attach(const struct scsi_host_template *tpnt, int unit,
  1119. struct sym_device *dev)
  1120. {
  1121. struct sym_data *sym_data;
  1122. struct sym_hcb *np = NULL;
  1123. struct Scsi_Host *shost = NULL;
  1124. struct pci_dev *pdev = dev->pdev;
  1125. unsigned long flags;
  1126. struct sym_fw *fw;
  1127. int do_free_irq = 0;
  1128. printk(KERN_INFO "sym%d: <%s> rev 0x%x at pci %s irq %u\n",
  1129. unit, dev->chip.name, pdev->revision, pci_name(pdev),
  1130. pdev->irq);
  1131. /*
  1132. * Get the firmware for this chip.
  1133. */
  1134. fw = sym_find_firmware(&dev->chip);
  1135. if (!fw)
  1136. goto attach_failed;
  1137. shost = scsi_host_alloc(tpnt, sizeof(*sym_data));
  1138. if (!shost)
  1139. goto attach_failed;
  1140. sym_data = shost_priv(shost);
  1141. /*
  1142. * Allocate immediately the host control block,
  1143. * since we are only expecting to succeed. :)
  1144. * We keep track in the HCB of all the resources that
  1145. * are to be released on error.
  1146. */
  1147. np = __sym_calloc_dma(&pdev->dev, sizeof(*np), "HCB");
  1148. if (!np)
  1149. goto attach_failed;
  1150. np->bus_dmat = &pdev->dev; /* Result in 1 DMA pool per HBA */
  1151. sym_data->ncb = np;
  1152. sym_data->pdev = pdev;
  1153. np->s.host = shost;
  1154. pci_set_drvdata(pdev, shost);
  1155. /*
  1156. * Copy some useful infos to the HCB.
  1157. */
  1158. np->hcb_ba = vtobus(np);
  1159. np->verbose = sym_driver_setup.verbose;
  1160. np->s.unit = unit;
  1161. np->features = dev->chip.features;
  1162. np->clock_divn = dev->chip.nr_divisor;
  1163. np->maxoffs = dev->chip.offset_max;
  1164. np->maxburst = dev->chip.burst_max;
  1165. np->myaddr = dev->host_id;
  1166. np->mmio_ba = (u32)dev->mmio_base;
  1167. np->ram_ba = (u32)dev->ram_base;
  1168. np->s.ioaddr = dev->s.ioaddr;
  1169. np->s.ramaddr = dev->s.ramaddr;
  1170. /*
  1171. * Edit its name.
  1172. */
  1173. strscpy(np->s.chip_name, dev->chip.name, sizeof(np->s.chip_name));
  1174. sprintf(np->s.inst_name, "sym%d", np->s.unit);
  1175. if ((SYM_CONF_DMA_ADDRESSING_MODE > 0) && (np->features & FE_DAC) &&
  1176. !dma_set_mask(&pdev->dev, DMA_DAC_MASK)) {
  1177. set_dac(np);
  1178. } else if (dma_set_mask(&pdev->dev, DMA_BIT_MASK(32))) {
  1179. printf_warning("%s: No suitable DMA available\n", sym_name(np));
  1180. goto attach_failed;
  1181. }
  1182. if (sym_hcb_attach(shost, fw, dev->nvram))
  1183. goto attach_failed;
  1184. /*
  1185. * Install the interrupt handler.
  1186. * If we synchonize the C code with SCRIPTS on interrupt,
  1187. * we do not want to share the INTR line at all.
  1188. */
  1189. if (request_irq(pdev->irq, sym53c8xx_intr, IRQF_SHARED, NAME53C8XX,
  1190. shost)) {
  1191. printf_err("%s: request irq %u failure\n",
  1192. sym_name(np), pdev->irq);
  1193. goto attach_failed;
  1194. }
  1195. do_free_irq = 1;
  1196. /*
  1197. * After SCSI devices have been opened, we cannot
  1198. * reset the bus safely, so we do it here.
  1199. */
  1200. spin_lock_irqsave(shost->host_lock, flags);
  1201. if (sym_reset_scsi_bus(np, 0))
  1202. goto reset_failed;
  1203. /*
  1204. * Start the SCRIPTS.
  1205. */
  1206. sym_start_up(shost, 1);
  1207. /*
  1208. * Start the timer daemon
  1209. */
  1210. timer_setup(&np->s.timer, sym53c8xx_timer, 0);
  1211. np->s.lasttime=0;
  1212. sym_timer (np);
  1213. /*
  1214. * Fill Linux host instance structure
  1215. * and return success.
  1216. */
  1217. shost->max_channel = 0;
  1218. shost->this_id = np->myaddr;
  1219. shost->max_id = np->maxwide ? 16 : 8;
  1220. shost->max_lun = SYM_CONF_MAX_LUN;
  1221. shost->unique_id = pci_resource_start(pdev, 0);
  1222. shost->cmd_per_lun = SYM_CONF_MAX_TAG;
  1223. shost->can_queue = (SYM_CONF_MAX_START-2);
  1224. shost->sg_tablesize = SYM_CONF_MAX_SG;
  1225. shost->max_cmd_len = 16;
  1226. BUG_ON(sym2_transport_template == NULL);
  1227. shost->transportt = sym2_transport_template;
  1228. /* 53c896 rev 1 errata: DMA may not cross 16MB boundary */
  1229. if (pdev->device == PCI_DEVICE_ID_NCR_53C896 && pdev->revision < 2)
  1230. shost->dma_boundary = 0xFFFFFF;
  1231. spin_unlock_irqrestore(shost->host_lock, flags);
  1232. return shost;
  1233. reset_failed:
  1234. printf_err("%s: FATAL ERROR: CHECK SCSI BUS - CABLES, "
  1235. "TERMINATION, DEVICE POWER etc.!\n", sym_name(np));
  1236. spin_unlock_irqrestore(shost->host_lock, flags);
  1237. attach_failed:
  1238. printf_info("sym%d: giving up ...\n", unit);
  1239. if (np)
  1240. sym_free_resources(np, pdev, do_free_irq);
  1241. else
  1242. sym_iounmap_device(dev);
  1243. if (shost)
  1244. scsi_host_put(shost);
  1245. return NULL;
  1246. }
  1247. /*
  1248. * Detect and try to read SYMBIOS and TEKRAM NVRAM.
  1249. */
  1250. #if SYM_CONF_NVRAM_SUPPORT
  1251. static void sym_get_nvram(struct sym_device *devp, struct sym_nvram *nvp)
  1252. {
  1253. devp->nvram = nvp;
  1254. nvp->type = 0;
  1255. sym_read_nvram(devp, nvp);
  1256. }
  1257. #else
  1258. static inline void sym_get_nvram(struct sym_device *devp, struct sym_nvram *nvp)
  1259. {
  1260. }
  1261. #endif /* SYM_CONF_NVRAM_SUPPORT */
  1262. static int sym_check_supported(struct sym_device *device)
  1263. {
  1264. struct sym_chip *chip;
  1265. struct pci_dev *pdev = device->pdev;
  1266. unsigned long io_port = pci_resource_start(pdev, 0);
  1267. int i;
  1268. /*
  1269. * If user excluded this chip, do not initialize it.
  1270. * I hate this code so much. Must kill it.
  1271. */
  1272. if (io_port) {
  1273. for (i = 0 ; i < 8 ; i++) {
  1274. if (sym_driver_setup.excludes[i] == io_port)
  1275. return -ENODEV;
  1276. }
  1277. }
  1278. /*
  1279. * Check if the chip is supported. Then copy the chip description
  1280. * to our device structure so we can make it match the actual device
  1281. * and options.
  1282. */
  1283. chip = sym_lookup_chip_table(pdev->device, pdev->revision);
  1284. if (!chip) {
  1285. dev_info(&pdev->dev, "device not supported\n");
  1286. return -ENODEV;
  1287. }
  1288. memcpy(&device->chip, chip, sizeof(device->chip));
  1289. return 0;
  1290. }
  1291. /*
  1292. * Ignore Symbios chips controlled by various RAID controllers.
  1293. * These controllers set value 0x52414944 at RAM end - 16.
  1294. */
  1295. static int sym_check_raid(struct sym_device *device)
  1296. {
  1297. unsigned int ram_size, ram_val;
  1298. if (!device->s.ramaddr)
  1299. return 0;
  1300. if (device->chip.features & FE_RAM8K)
  1301. ram_size = 8192;
  1302. else
  1303. ram_size = 4096;
  1304. ram_val = readl(device->s.ramaddr + ram_size - 16);
  1305. if (ram_val != 0x52414944)
  1306. return 0;
  1307. dev_info(&device->pdev->dev,
  1308. "not initializing, driven by RAID controller.\n");
  1309. return -ENODEV;
  1310. }
  1311. static int sym_set_workarounds(struct sym_device *device)
  1312. {
  1313. struct sym_chip *chip = &device->chip;
  1314. struct pci_dev *pdev = device->pdev;
  1315. u_short status_reg;
  1316. /*
  1317. * (ITEM 12 of a DEL about the 896 I haven't yet).
  1318. * We must ensure the chip will use WRITE AND INVALIDATE.
  1319. * The revision number limit is for now arbitrary.
  1320. */
  1321. if (pdev->device == PCI_DEVICE_ID_NCR_53C896 && pdev->revision < 0x4) {
  1322. chip->features |= (FE_WRIE | FE_CLSE);
  1323. }
  1324. /* If the chip can do Memory Write Invalidate, enable it */
  1325. if (chip->features & FE_WRIE) {
  1326. if (pci_set_mwi(pdev))
  1327. return -ENODEV;
  1328. }
  1329. /*
  1330. * Work around for errant bit in 895A. The 66Mhz
  1331. * capable bit is set erroneously. Clear this bit.
  1332. * (Item 1 DEL 533)
  1333. *
  1334. * Make sure Config space and Features agree.
  1335. *
  1336. * Recall: writes are not normal to status register -
  1337. * write a 1 to clear and a 0 to leave unchanged.
  1338. * Can only reset bits.
  1339. */
  1340. pci_read_config_word(pdev, PCI_STATUS, &status_reg);
  1341. if (chip->features & FE_66MHZ) {
  1342. if (!(status_reg & PCI_STATUS_66MHZ))
  1343. chip->features &= ~FE_66MHZ;
  1344. } else {
  1345. if (status_reg & PCI_STATUS_66MHZ) {
  1346. status_reg = PCI_STATUS_66MHZ;
  1347. pci_write_config_word(pdev, PCI_STATUS, status_reg);
  1348. pci_read_config_word(pdev, PCI_STATUS, &status_reg);
  1349. }
  1350. }
  1351. return 0;
  1352. }
  1353. /*
  1354. * Map HBA registers and on-chip SRAM (if present).
  1355. */
  1356. static int sym_iomap_device(struct sym_device *device)
  1357. {
  1358. struct pci_dev *pdev = device->pdev;
  1359. struct pci_bus_region bus_addr;
  1360. int i = 2;
  1361. pcibios_resource_to_bus(pdev->bus, &bus_addr, &pdev->resource[1]);
  1362. device->mmio_base = bus_addr.start;
  1363. if (device->chip.features & FE_RAM) {
  1364. /*
  1365. * If the BAR is 64-bit, resource 2 will be occupied by the
  1366. * upper 32 bits
  1367. */
  1368. if (!pdev->resource[i].flags)
  1369. i++;
  1370. pcibios_resource_to_bus(pdev->bus, &bus_addr,
  1371. &pdev->resource[i]);
  1372. device->ram_base = bus_addr.start;
  1373. }
  1374. #ifdef CONFIG_SCSI_SYM53C8XX_MMIO
  1375. if (device->mmio_base)
  1376. device->s.ioaddr = pci_iomap(pdev, 1,
  1377. pci_resource_len(pdev, 1));
  1378. #endif
  1379. if (!device->s.ioaddr)
  1380. device->s.ioaddr = pci_iomap(pdev, 0,
  1381. pci_resource_len(pdev, 0));
  1382. if (!device->s.ioaddr) {
  1383. dev_err(&pdev->dev, "could not map registers; giving up.\n");
  1384. return -EIO;
  1385. }
  1386. if (device->ram_base) {
  1387. device->s.ramaddr = pci_iomap(pdev, i,
  1388. pci_resource_len(pdev, i));
  1389. if (!device->s.ramaddr) {
  1390. dev_warn(&pdev->dev,
  1391. "could not map SRAM; continuing anyway.\n");
  1392. device->ram_base = 0;
  1393. }
  1394. }
  1395. return 0;
  1396. }
  1397. /*
  1398. * The NCR PQS and PDS cards are constructed as a DEC bridge
  1399. * behind which sits a proprietary NCR memory controller and
  1400. * either four or two 53c875s as separate devices. We can tell
  1401. * if an 875 is part of a PQS/PDS or not since if it is, it will
  1402. * be on the same bus as the memory controller. In its usual
  1403. * mode of operation, the 875s are slaved to the memory
  1404. * controller for all transfers. To operate with the Linux
  1405. * driver, the memory controller is disabled and the 875s
  1406. * freed to function independently. The only wrinkle is that
  1407. * the preset SCSI ID (which may be zero) must be read in from
  1408. * a special configuration space register of the 875.
  1409. */
  1410. static void sym_config_pqs(struct pci_dev *pdev, struct sym_device *sym_dev)
  1411. {
  1412. int slot;
  1413. u8 tmp;
  1414. for (slot = 0; slot < 256; slot++) {
  1415. struct pci_dev *memc = pci_get_slot(pdev->bus, slot);
  1416. if (!memc || memc->vendor != 0x101a || memc->device == 0x0009) {
  1417. pci_dev_put(memc);
  1418. continue;
  1419. }
  1420. /* bit 1: allow individual 875 configuration */
  1421. pci_read_config_byte(memc, 0x44, &tmp);
  1422. if ((tmp & 0x2) == 0) {
  1423. tmp |= 0x2;
  1424. pci_write_config_byte(memc, 0x44, tmp);
  1425. }
  1426. /* bit 2: drive individual 875 interrupts to the bus */
  1427. pci_read_config_byte(memc, 0x45, &tmp);
  1428. if ((tmp & 0x4) == 0) {
  1429. tmp |= 0x4;
  1430. pci_write_config_byte(memc, 0x45, tmp);
  1431. }
  1432. pci_dev_put(memc);
  1433. break;
  1434. }
  1435. pci_read_config_byte(pdev, 0x84, &tmp);
  1436. sym_dev->host_id = tmp;
  1437. }
  1438. /*
  1439. * Called before unloading the module.
  1440. * Detach the host.
  1441. * We have to free resources and halt the NCR chip.
  1442. */
  1443. static int sym_detach(struct Scsi_Host *shost, struct pci_dev *pdev)
  1444. {
  1445. struct sym_hcb *np = sym_get_hcb(shost);
  1446. printk("%s: detaching ...\n", sym_name(np));
  1447. timer_delete_sync(&np->s.timer);
  1448. /*
  1449. * Reset NCR chip.
  1450. * We should use sym_soft_reset(), but we don't want to do
  1451. * so, since we may not be safe if interrupts occur.
  1452. */
  1453. printk("%s: resetting chip\n", sym_name(np));
  1454. OUTB(np, nc_istat, SRST);
  1455. INB(np, nc_mbox1);
  1456. udelay(10);
  1457. OUTB(np, nc_istat, 0);
  1458. sym_free_resources(np, pdev, 1);
  1459. scsi_host_put(shost);
  1460. return 1;
  1461. }
  1462. /*
  1463. * Driver host template.
  1464. */
  1465. static const struct scsi_host_template sym2_template = {
  1466. .module = THIS_MODULE,
  1467. .name = "sym53c8xx",
  1468. .info = sym53c8xx_info,
  1469. .cmd_size = sizeof(struct sym_ucmd),
  1470. .queuecommand = sym53c8xx_queue_command,
  1471. .sdev_init = sym53c8xx_sdev_init,
  1472. .sdev_configure = sym53c8xx_sdev_configure,
  1473. .sdev_destroy = sym53c8xx_sdev_destroy,
  1474. .eh_abort_handler = sym53c8xx_eh_abort_handler,
  1475. .eh_target_reset_handler = sym53c8xx_eh_target_reset_handler,
  1476. .eh_bus_reset_handler = sym53c8xx_eh_bus_reset_handler,
  1477. .eh_host_reset_handler = sym53c8xx_eh_host_reset_handler,
  1478. .this_id = 7,
  1479. .max_sectors = 0xFFFF,
  1480. #ifdef SYM_LINUX_PROC_INFO_SUPPORT
  1481. .show_info = sym_show_info,
  1482. #ifdef SYM_LINUX_USER_COMMAND_SUPPORT
  1483. .write_info = sym_user_command,
  1484. #endif
  1485. .proc_name = NAME53C8XX,
  1486. #endif
  1487. };
  1488. static int attach_count;
  1489. static int sym2_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
  1490. {
  1491. struct sym_device sym_dev;
  1492. struct sym_nvram nvram;
  1493. struct Scsi_Host *shost;
  1494. int do_iounmap = 0;
  1495. int do_disable_device = 1;
  1496. memset(&sym_dev, 0, sizeof(sym_dev));
  1497. memset(&nvram, 0, sizeof(nvram));
  1498. sym_dev.pdev = pdev;
  1499. sym_dev.host_id = SYM_SETUP_HOST_ID;
  1500. if (pci_enable_device(pdev))
  1501. goto leave;
  1502. pci_set_master(pdev);
  1503. if (pci_request_regions(pdev, NAME53C8XX))
  1504. goto disable;
  1505. if (sym_check_supported(&sym_dev))
  1506. goto free;
  1507. if (sym_iomap_device(&sym_dev))
  1508. goto free;
  1509. do_iounmap = 1;
  1510. if (sym_check_raid(&sym_dev)) {
  1511. do_disable_device = 0; /* Don't disable the device */
  1512. goto free;
  1513. }
  1514. if (sym_set_workarounds(&sym_dev))
  1515. goto free;
  1516. sym_config_pqs(pdev, &sym_dev);
  1517. sym_get_nvram(&sym_dev, &nvram);
  1518. do_iounmap = 0; /* Don't sym_iounmap_device() after sym_attach(). */
  1519. shost = sym_attach(&sym2_template, attach_count, &sym_dev);
  1520. if (!shost)
  1521. goto free;
  1522. if (scsi_add_host(shost, &pdev->dev))
  1523. goto detach;
  1524. scsi_scan_host(shost);
  1525. attach_count++;
  1526. return 0;
  1527. detach:
  1528. sym_detach(pci_get_drvdata(pdev), pdev);
  1529. free:
  1530. if (do_iounmap)
  1531. sym_iounmap_device(&sym_dev);
  1532. pci_release_regions(pdev);
  1533. disable:
  1534. if (do_disable_device)
  1535. pci_disable_device(pdev);
  1536. leave:
  1537. return -ENODEV;
  1538. }
  1539. static void sym2_remove(struct pci_dev *pdev)
  1540. {
  1541. struct Scsi_Host *shost = pci_get_drvdata(pdev);
  1542. scsi_remove_host(shost);
  1543. sym_detach(shost, pdev);
  1544. pci_release_regions(pdev);
  1545. pci_disable_device(pdev);
  1546. attach_count--;
  1547. }
  1548. /**
  1549. * sym2_io_error_detected() - called when PCI error is detected
  1550. * @pdev: pointer to PCI device
  1551. * @state: current state of the PCI slot
  1552. */
  1553. static pci_ers_result_t sym2_io_error_detected(struct pci_dev *pdev,
  1554. pci_channel_state_t state)
  1555. {
  1556. /* If slot is permanently frozen, turn everything off */
  1557. if (state == pci_channel_io_perm_failure) {
  1558. sym2_remove(pdev);
  1559. return PCI_ERS_RESULT_DISCONNECT;
  1560. }
  1561. disable_irq(pdev->irq);
  1562. pci_disable_device(pdev);
  1563. /* Request that MMIO be enabled, so register dump can be taken. */
  1564. return PCI_ERS_RESULT_CAN_RECOVER;
  1565. }
  1566. /**
  1567. * sym2_io_slot_dump - Enable MMIO and dump debug registers
  1568. * @pdev: pointer to PCI device
  1569. */
  1570. static pci_ers_result_t sym2_io_slot_dump(struct pci_dev *pdev)
  1571. {
  1572. struct Scsi_Host *shost = pci_get_drvdata(pdev);
  1573. sym_dump_registers(shost);
  1574. /* Request a slot reset. */
  1575. return PCI_ERS_RESULT_NEED_RESET;
  1576. }
  1577. /**
  1578. * sym2_reset_workarounds - hardware-specific work-arounds
  1579. * @pdev: pointer to PCI device
  1580. *
  1581. * This routine is similar to sym_set_workarounds(), except
  1582. * that, at this point, we already know that the device was
  1583. * successfully initialized at least once before, and so most
  1584. * of the steps taken there are un-needed here.
  1585. */
  1586. static void sym2_reset_workarounds(struct pci_dev *pdev)
  1587. {
  1588. u_short status_reg;
  1589. struct sym_chip *chip;
  1590. chip = sym_lookup_chip_table(pdev->device, pdev->revision);
  1591. /* Work around for errant bit in 895A, in a fashion
  1592. * similar to what is done in sym_set_workarounds().
  1593. */
  1594. pci_read_config_word(pdev, PCI_STATUS, &status_reg);
  1595. if (!(chip->features & FE_66MHZ) && (status_reg & PCI_STATUS_66MHZ)) {
  1596. status_reg = PCI_STATUS_66MHZ;
  1597. pci_write_config_word(pdev, PCI_STATUS, status_reg);
  1598. pci_read_config_word(pdev, PCI_STATUS, &status_reg);
  1599. }
  1600. }
  1601. /**
  1602. * sym2_io_slot_reset() - called when the pci bus has been reset.
  1603. * @pdev: pointer to PCI device
  1604. *
  1605. * Restart the card from scratch.
  1606. */
  1607. static pci_ers_result_t sym2_io_slot_reset(struct pci_dev *pdev)
  1608. {
  1609. struct Scsi_Host *shost = pci_get_drvdata(pdev);
  1610. struct sym_hcb *np = sym_get_hcb(shost);
  1611. printk(KERN_INFO "%s: recovering from a PCI slot reset\n",
  1612. sym_name(np));
  1613. if (pci_enable_device(pdev)) {
  1614. printk(KERN_ERR "%s: Unable to enable after PCI reset\n",
  1615. sym_name(np));
  1616. return PCI_ERS_RESULT_DISCONNECT;
  1617. }
  1618. pci_set_master(pdev);
  1619. enable_irq(pdev->irq);
  1620. /* If the chip can do Memory Write Invalidate, enable it */
  1621. if (np->features & FE_WRIE) {
  1622. if (pci_set_mwi(pdev))
  1623. return PCI_ERS_RESULT_DISCONNECT;
  1624. }
  1625. /* Perform work-arounds, analogous to sym_set_workarounds() */
  1626. sym2_reset_workarounds(pdev);
  1627. /* Perform host reset only on one instance of the card */
  1628. if (PCI_FUNC(pdev->devfn) == 0) {
  1629. if (sym_reset_scsi_bus(np, 0)) {
  1630. printk(KERN_ERR "%s: Unable to reset scsi host\n",
  1631. sym_name(np));
  1632. return PCI_ERS_RESULT_DISCONNECT;
  1633. }
  1634. sym_start_up(shost, 1);
  1635. }
  1636. return PCI_ERS_RESULT_RECOVERED;
  1637. }
  1638. /**
  1639. * sym2_io_resume() - resume normal ops after PCI reset
  1640. * @pdev: pointer to PCI device
  1641. *
  1642. * Called when the error recovery driver tells us that its
  1643. * OK to resume normal operation. Use completion to allow
  1644. * halted scsi ops to resume.
  1645. */
  1646. static void sym2_io_resume(struct pci_dev *pdev)
  1647. {
  1648. struct Scsi_Host *shost = pci_get_drvdata(pdev);
  1649. struct sym_data *sym_data = shost_priv(shost);
  1650. spin_lock_irq(shost->host_lock);
  1651. if (sym_data->io_reset)
  1652. complete(sym_data->io_reset);
  1653. spin_unlock_irq(shost->host_lock);
  1654. }
  1655. static void sym2_get_signalling(struct Scsi_Host *shost)
  1656. {
  1657. struct sym_hcb *np = sym_get_hcb(shost);
  1658. enum spi_signal_type type;
  1659. switch (np->scsi_mode) {
  1660. case SMODE_SE:
  1661. type = SPI_SIGNAL_SE;
  1662. break;
  1663. case SMODE_LVD:
  1664. type = SPI_SIGNAL_LVD;
  1665. break;
  1666. case SMODE_HVD:
  1667. type = SPI_SIGNAL_HVD;
  1668. break;
  1669. default:
  1670. type = SPI_SIGNAL_UNKNOWN;
  1671. break;
  1672. }
  1673. spi_signalling(shost) = type;
  1674. }
  1675. static void sym2_set_offset(struct scsi_target *starget, int offset)
  1676. {
  1677. struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
  1678. struct sym_hcb *np = sym_get_hcb(shost);
  1679. struct sym_tcb *tp = &np->target[starget->id];
  1680. tp->tgoal.offset = offset;
  1681. tp->tgoal.check_nego = 1;
  1682. }
  1683. static void sym2_set_period(struct scsi_target *starget, int period)
  1684. {
  1685. struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
  1686. struct sym_hcb *np = sym_get_hcb(shost);
  1687. struct sym_tcb *tp = &np->target[starget->id];
  1688. /* have to have DT for these transfers, but DT will also
  1689. * set width, so check that this is allowed */
  1690. if (period <= np->minsync && spi_width(starget))
  1691. tp->tgoal.dt = 1;
  1692. tp->tgoal.period = period;
  1693. tp->tgoal.check_nego = 1;
  1694. }
  1695. static void sym2_set_width(struct scsi_target *starget, int width)
  1696. {
  1697. struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
  1698. struct sym_hcb *np = sym_get_hcb(shost);
  1699. struct sym_tcb *tp = &np->target[starget->id];
  1700. /* It is illegal to have DT set on narrow transfers. If DT is
  1701. * clear, we must also clear IU and QAS. */
  1702. if (width == 0)
  1703. tp->tgoal.iu = tp->tgoal.dt = tp->tgoal.qas = 0;
  1704. tp->tgoal.width = width;
  1705. tp->tgoal.check_nego = 1;
  1706. }
  1707. static void sym2_set_dt(struct scsi_target *starget, int dt)
  1708. {
  1709. struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
  1710. struct sym_hcb *np = sym_get_hcb(shost);
  1711. struct sym_tcb *tp = &np->target[starget->id];
  1712. /* We must clear QAS and IU if DT is clear */
  1713. if (dt)
  1714. tp->tgoal.dt = 1;
  1715. else
  1716. tp->tgoal.iu = tp->tgoal.dt = tp->tgoal.qas = 0;
  1717. tp->tgoal.check_nego = 1;
  1718. }
  1719. #if 0
  1720. static void sym2_set_iu(struct scsi_target *starget, int iu)
  1721. {
  1722. struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
  1723. struct sym_hcb *np = sym_get_hcb(shost);
  1724. struct sym_tcb *tp = &np->target[starget->id];
  1725. if (iu)
  1726. tp->tgoal.iu = tp->tgoal.dt = 1;
  1727. else
  1728. tp->tgoal.iu = 0;
  1729. tp->tgoal.check_nego = 1;
  1730. }
  1731. static void sym2_set_qas(struct scsi_target *starget, int qas)
  1732. {
  1733. struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
  1734. struct sym_hcb *np = sym_get_hcb(shost);
  1735. struct sym_tcb *tp = &np->target[starget->id];
  1736. if (qas)
  1737. tp->tgoal.dt = tp->tgoal.qas = 1;
  1738. else
  1739. tp->tgoal.qas = 0;
  1740. tp->tgoal.check_nego = 1;
  1741. }
  1742. #endif
  1743. static struct spi_function_template sym2_transport_functions = {
  1744. .set_offset = sym2_set_offset,
  1745. .show_offset = 1,
  1746. .set_period = sym2_set_period,
  1747. .show_period = 1,
  1748. .set_width = sym2_set_width,
  1749. .show_width = 1,
  1750. .set_dt = sym2_set_dt,
  1751. .show_dt = 1,
  1752. #if 0
  1753. .set_iu = sym2_set_iu,
  1754. .show_iu = 1,
  1755. .set_qas = sym2_set_qas,
  1756. .show_qas = 1,
  1757. #endif
  1758. .get_signalling = sym2_get_signalling,
  1759. };
  1760. static const struct pci_device_id sym2_id_table[] = {
  1761. { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_NCR_53C810,
  1762. PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0UL },
  1763. { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_NCR_53C820,
  1764. PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0UL }, /* new */
  1765. { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_NCR_53C825,
  1766. PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0UL },
  1767. { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_NCR_53C815,
  1768. PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0UL },
  1769. { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_53C810AP,
  1770. PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0UL }, /* new */
  1771. { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_NCR_53C860,
  1772. PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0UL },
  1773. { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_53C1510,
  1774. PCI_ANY_ID, PCI_ANY_ID, PCI_CLASS_STORAGE_SCSI<<8, 0xffff00, 0UL },
  1775. { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_NCR_53C896,
  1776. PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0UL },
  1777. { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_NCR_53C895,
  1778. PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0UL },
  1779. { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_NCR_53C885,
  1780. PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0UL },
  1781. { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_NCR_53C875,
  1782. PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0UL },
  1783. { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_NCR_53C1510,
  1784. PCI_ANY_ID, PCI_ANY_ID, PCI_CLASS_STORAGE_SCSI<<8, 0xffff00, 0UL }, /* new */
  1785. { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_53C895A,
  1786. PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0UL },
  1787. { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_53C875A,
  1788. PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0UL },
  1789. { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_53C1010_33,
  1790. PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0UL },
  1791. { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_53C1010_66,
  1792. PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0UL },
  1793. { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_NCR_53C875J,
  1794. PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0UL },
  1795. { 0, }
  1796. };
  1797. MODULE_DEVICE_TABLE(pci, sym2_id_table);
  1798. static const struct pci_error_handlers sym2_err_handler = {
  1799. .error_detected = sym2_io_error_detected,
  1800. .mmio_enabled = sym2_io_slot_dump,
  1801. .slot_reset = sym2_io_slot_reset,
  1802. .resume = sym2_io_resume,
  1803. };
  1804. static struct pci_driver sym2_driver = {
  1805. .name = NAME53C8XX,
  1806. .id_table = sym2_id_table,
  1807. .probe = sym2_probe,
  1808. .remove = sym2_remove,
  1809. .err_handler = &sym2_err_handler,
  1810. };
  1811. static int __init sym2_init(void)
  1812. {
  1813. int error;
  1814. sym2_setup_params();
  1815. sym2_transport_template = spi_attach_transport(&sym2_transport_functions);
  1816. if (!sym2_transport_template)
  1817. return -ENODEV;
  1818. error = pci_register_driver(&sym2_driver);
  1819. if (error)
  1820. spi_release_transport(sym2_transport_template);
  1821. return error;
  1822. }
  1823. static void __exit sym2_exit(void)
  1824. {
  1825. pci_unregister_driver(&sym2_driver);
  1826. spi_release_transport(sym2_transport_template);
  1827. }
  1828. module_init(sym2_init);
  1829. module_exit(sym2_exit);