sym_glue.c 53 KB

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