sym_glue.c 53 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084
  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(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. /*
  481. * Linux entry point of the interrupt handler.
  482. */
  483. static irqreturn_t sym53c8xx_intr(int irq, void *dev_id)
  484. {
  485. struct Scsi_Host *shost = dev_id;
  486. struct sym_data *sym_data = shost_priv(shost);
  487. irqreturn_t result;
  488. /* Avoid spinloop trying to handle interrupts on frozen device */
  489. if (pci_channel_offline(sym_data->pdev))
  490. return IRQ_NONE;
  491. if (DEBUG_FLAGS & DEBUG_TINY) printf_debug ("[");
  492. spin_lock(shost->host_lock);
  493. result = sym_interrupt(shost);
  494. spin_unlock(shost->host_lock);
  495. if (DEBUG_FLAGS & DEBUG_TINY) printf_debug ("]\n");
  496. return result;
  497. }
  498. /*
  499. * Linux entry point of the timer handler
  500. */
  501. static void sym53c8xx_timer(unsigned long npref)
  502. {
  503. struct sym_hcb *np = (struct sym_hcb *)npref;
  504. unsigned long flags;
  505. spin_lock_irqsave(np->s.host->host_lock, flags);
  506. sym_timer(np);
  507. spin_unlock_irqrestore(np->s.host->host_lock, flags);
  508. }
  509. /*
  510. * What the eh thread wants us to perform.
  511. */
  512. #define SYM_EH_ABORT 0
  513. #define SYM_EH_DEVICE_RESET 1
  514. #define SYM_EH_BUS_RESET 2
  515. #define SYM_EH_HOST_RESET 3
  516. /*
  517. * Generic method for our eh processing.
  518. * The 'op' argument tells what we have to do.
  519. */
  520. static int sym_eh_handler(int op, char *opname, struct scsi_cmnd *cmd)
  521. {
  522. struct sym_ucmd *ucmd = SYM_UCMD_PTR(cmd);
  523. struct Scsi_Host *shost = cmd->device->host;
  524. struct sym_data *sym_data = shost_priv(shost);
  525. struct pci_dev *pdev = sym_data->pdev;
  526. struct sym_hcb *np = sym_data->ncb;
  527. SYM_QUEHEAD *qp;
  528. int cmd_queued = 0;
  529. int sts = -1;
  530. struct completion eh_done;
  531. scmd_printk(KERN_WARNING, cmd, "%s operation started\n", opname);
  532. /* We may be in an error condition because the PCI bus
  533. * went down. In this case, we need to wait until the
  534. * PCI bus is reset, the card is reset, and only then
  535. * proceed with the scsi error recovery. There's no
  536. * point in hurrying; take a leisurely wait.
  537. */
  538. #define WAIT_FOR_PCI_RECOVERY 35
  539. if (pci_channel_offline(pdev)) {
  540. int finished_reset = 0;
  541. init_completion(&eh_done);
  542. spin_lock_irq(shost->host_lock);
  543. /* Make sure we didn't race */
  544. if (pci_channel_offline(pdev)) {
  545. BUG_ON(sym_data->io_reset);
  546. sym_data->io_reset = &eh_done;
  547. } else {
  548. finished_reset = 1;
  549. }
  550. spin_unlock_irq(shost->host_lock);
  551. if (!finished_reset)
  552. finished_reset = wait_for_completion_timeout
  553. (sym_data->io_reset,
  554. WAIT_FOR_PCI_RECOVERY*HZ);
  555. spin_lock_irq(shost->host_lock);
  556. sym_data->io_reset = NULL;
  557. spin_unlock_irq(shost->host_lock);
  558. if (!finished_reset)
  559. return SCSI_FAILED;
  560. }
  561. spin_lock_irq(shost->host_lock);
  562. /* This one is queued in some place -> to wait for completion */
  563. FOR_EACH_QUEUED_ELEMENT(&np->busy_ccbq, qp) {
  564. struct sym_ccb *cp = sym_que_entry(qp, struct sym_ccb, link_ccbq);
  565. if (cp->cmd == cmd) {
  566. cmd_queued = 1;
  567. break;
  568. }
  569. }
  570. /* Try to proceed the operation we have been asked for */
  571. sts = -1;
  572. switch(op) {
  573. case SYM_EH_ABORT:
  574. sts = sym_abort_scsiio(np, cmd, 1);
  575. break;
  576. case SYM_EH_DEVICE_RESET:
  577. sts = sym_reset_scsi_target(np, cmd->device->id);
  578. break;
  579. case SYM_EH_BUS_RESET:
  580. sym_reset_scsi_bus(np, 1);
  581. sts = 0;
  582. break;
  583. case SYM_EH_HOST_RESET:
  584. sym_reset_scsi_bus(np, 0);
  585. sym_start_up(shost, 1);
  586. sts = 0;
  587. break;
  588. default:
  589. break;
  590. }
  591. /* On error, restore everything and cross fingers :) */
  592. if (sts)
  593. cmd_queued = 0;
  594. if (cmd_queued) {
  595. init_completion(&eh_done);
  596. ucmd->eh_done = &eh_done;
  597. spin_unlock_irq(shost->host_lock);
  598. if (!wait_for_completion_timeout(&eh_done, 5*HZ)) {
  599. ucmd->eh_done = NULL;
  600. sts = -2;
  601. }
  602. } else {
  603. spin_unlock_irq(shost->host_lock);
  604. }
  605. dev_warn(&cmd->device->sdev_gendev, "%s operation %s.\n", opname,
  606. sts==0 ? "complete" :sts==-2 ? "timed-out" : "failed");
  607. return sts ? SCSI_FAILED : SCSI_SUCCESS;
  608. }
  609. /*
  610. * Error handlers called from the eh thread (one thread per HBA).
  611. */
  612. static int sym53c8xx_eh_abort_handler(struct scsi_cmnd *cmd)
  613. {
  614. return sym_eh_handler(SYM_EH_ABORT, "ABORT", cmd);
  615. }
  616. static int sym53c8xx_eh_device_reset_handler(struct scsi_cmnd *cmd)
  617. {
  618. return sym_eh_handler(SYM_EH_DEVICE_RESET, "DEVICE RESET", cmd);
  619. }
  620. static int sym53c8xx_eh_bus_reset_handler(struct scsi_cmnd *cmd)
  621. {
  622. return sym_eh_handler(SYM_EH_BUS_RESET, "BUS RESET", cmd);
  623. }
  624. static int sym53c8xx_eh_host_reset_handler(struct scsi_cmnd *cmd)
  625. {
  626. return sym_eh_handler(SYM_EH_HOST_RESET, "HOST RESET", cmd);
  627. }
  628. /*
  629. * Tune device queuing depth, according to various limits.
  630. */
  631. static void sym_tune_dev_queuing(struct sym_tcb *tp, int lun, u_short reqtags)
  632. {
  633. struct sym_lcb *lp = sym_lp(tp, lun);
  634. u_short oldtags;
  635. if (!lp)
  636. return;
  637. oldtags = lp->s.reqtags;
  638. if (reqtags > lp->s.scdev_depth)
  639. reqtags = lp->s.scdev_depth;
  640. lp->s.reqtags = reqtags;
  641. if (reqtags != oldtags) {
  642. dev_info(&tp->starget->dev,
  643. "tagged command queuing %s, command queue depth %d.\n",
  644. lp->s.reqtags ? "enabled" : "disabled", reqtags);
  645. }
  646. }
  647. static int sym53c8xx_slave_alloc(struct scsi_device *sdev)
  648. {
  649. struct sym_hcb *np = sym_get_hcb(sdev->host);
  650. struct sym_tcb *tp = &np->target[sdev->id];
  651. struct sym_lcb *lp;
  652. if (sdev->id >= SYM_CONF_MAX_TARGET || sdev->lun >= SYM_CONF_MAX_LUN)
  653. return -ENXIO;
  654. tp->starget = sdev->sdev_target;
  655. /*
  656. * Fail the device init if the device is flagged NOSCAN at BOOT in
  657. * the NVRAM. This may speed up boot and maintain coherency with
  658. * BIOS device numbering. Clearing the flag allows the user to
  659. * rescan skipped devices later. We also return an error for
  660. * devices not flagged for SCAN LUNS in the NVRAM since some single
  661. * lun devices behave badly when asked for a non zero LUN.
  662. */
  663. if (tp->usrflags & SYM_SCAN_BOOT_DISABLED) {
  664. tp->usrflags &= ~SYM_SCAN_BOOT_DISABLED;
  665. starget_printk(KERN_INFO, tp->starget,
  666. "Scan at boot disabled in NVRAM\n");
  667. return -ENXIO;
  668. }
  669. if (tp->usrflags & SYM_SCAN_LUNS_DISABLED) {
  670. if (sdev->lun != 0)
  671. return -ENXIO;
  672. starget_printk(KERN_INFO, tp->starget,
  673. "Multiple LUNs disabled in NVRAM\n");
  674. }
  675. lp = sym_alloc_lcb(np, sdev->id, sdev->lun);
  676. if (!lp)
  677. return -ENOMEM;
  678. spi_min_period(tp->starget) = tp->usr_period;
  679. spi_max_width(tp->starget) = tp->usr_width;
  680. return 0;
  681. }
  682. /*
  683. * Linux entry point for device queue sizing.
  684. */
  685. static int sym53c8xx_slave_configure(struct scsi_device *sdev)
  686. {
  687. struct sym_hcb *np = sym_get_hcb(sdev->host);
  688. struct sym_tcb *tp = &np->target[sdev->id];
  689. struct sym_lcb *lp = sym_lp(tp, sdev->lun);
  690. int reqtags, depth_to_use;
  691. /*
  692. * Get user flags.
  693. */
  694. lp->curr_flags = lp->user_flags;
  695. /*
  696. * Select queue depth from driver setup.
  697. * Donnot use more than configured by user.
  698. * Use at least 2.
  699. * Donnot use more than our maximum.
  700. */
  701. reqtags = sym_driver_setup.max_tag;
  702. if (reqtags > tp->usrtags)
  703. reqtags = tp->usrtags;
  704. if (!sdev->tagged_supported)
  705. reqtags = 0;
  706. if (reqtags > SYM_CONF_MAX_TAG)
  707. reqtags = SYM_CONF_MAX_TAG;
  708. depth_to_use = reqtags ? reqtags : 2;
  709. scsi_adjust_queue_depth(sdev,
  710. sdev->tagged_supported ? MSG_SIMPLE_TAG : 0,
  711. depth_to_use);
  712. lp->s.scdev_depth = depth_to_use;
  713. sym_tune_dev_queuing(tp, sdev->lun, reqtags);
  714. if (!spi_initial_dv(sdev->sdev_target))
  715. spi_dv_device(sdev);
  716. return 0;
  717. }
  718. static void sym53c8xx_slave_destroy(struct scsi_device *sdev)
  719. {
  720. struct sym_hcb *np = sym_get_hcb(sdev->host);
  721. struct sym_lcb *lp = sym_lp(&np->target[sdev->id], sdev->lun);
  722. if (lp->itlq_tbl)
  723. sym_mfree_dma(lp->itlq_tbl, SYM_CONF_MAX_TASK * 4, "ITLQ_TBL");
  724. kfree(lp->cb_tags);
  725. sym_mfree_dma(lp, sizeof(*lp), "LCB");
  726. }
  727. /*
  728. * Linux entry point for info() function
  729. */
  730. static const char *sym53c8xx_info (struct Scsi_Host *host)
  731. {
  732. return SYM_DRIVER_NAME;
  733. }
  734. #ifdef SYM_LINUX_PROC_INFO_SUPPORT
  735. /*
  736. * Proc file system stuff
  737. *
  738. * A read operation returns adapter information.
  739. * A write operation is a control command.
  740. * The string is parsed in the driver code and the command is passed
  741. * to the sym_usercmd() function.
  742. */
  743. #ifdef SYM_LINUX_USER_COMMAND_SUPPORT
  744. struct sym_usrcmd {
  745. u_long target;
  746. u_long lun;
  747. u_long data;
  748. u_long cmd;
  749. };
  750. #define UC_SETSYNC 10
  751. #define UC_SETTAGS 11
  752. #define UC_SETDEBUG 12
  753. #define UC_SETWIDE 14
  754. #define UC_SETFLAG 15
  755. #define UC_SETVERBOSE 17
  756. #define UC_RESETDEV 18
  757. #define UC_CLEARDEV 19
  758. static void sym_exec_user_command (struct sym_hcb *np, struct sym_usrcmd *uc)
  759. {
  760. struct sym_tcb *tp;
  761. int t, l;
  762. switch (uc->cmd) {
  763. case 0: return;
  764. #ifdef SYM_LINUX_DEBUG_CONTROL_SUPPORT
  765. case UC_SETDEBUG:
  766. sym_debug_flags = uc->data;
  767. break;
  768. #endif
  769. case UC_SETVERBOSE:
  770. np->verbose = uc->data;
  771. break;
  772. default:
  773. /*
  774. * We assume that other commands apply to targets.
  775. * This should always be the case and avoid the below
  776. * 4 lines to be repeated 6 times.
  777. */
  778. for (t = 0; t < SYM_CONF_MAX_TARGET; t++) {
  779. if (!((uc->target >> t) & 1))
  780. continue;
  781. tp = &np->target[t];
  782. switch (uc->cmd) {
  783. case UC_SETSYNC:
  784. if (!uc->data || uc->data >= 255) {
  785. tp->tgoal.iu = tp->tgoal.dt =
  786. tp->tgoal.qas = 0;
  787. tp->tgoal.offset = 0;
  788. } else if (uc->data <= 9 && np->minsync_dt) {
  789. if (uc->data < np->minsync_dt)
  790. uc->data = np->minsync_dt;
  791. tp->tgoal.iu = tp->tgoal.dt =
  792. tp->tgoal.qas = 1;
  793. tp->tgoal.width = 1;
  794. tp->tgoal.period = uc->data;
  795. tp->tgoal.offset = np->maxoffs_dt;
  796. } else {
  797. if (uc->data < np->minsync)
  798. uc->data = np->minsync;
  799. tp->tgoal.iu = tp->tgoal.dt =
  800. tp->tgoal.qas = 0;
  801. tp->tgoal.period = uc->data;
  802. tp->tgoal.offset = np->maxoffs;
  803. }
  804. tp->tgoal.check_nego = 1;
  805. break;
  806. case UC_SETWIDE:
  807. tp->tgoal.width = uc->data ? 1 : 0;
  808. tp->tgoal.check_nego = 1;
  809. break;
  810. case UC_SETTAGS:
  811. for (l = 0; l < SYM_CONF_MAX_LUN; l++)
  812. sym_tune_dev_queuing(tp, l, uc->data);
  813. break;
  814. case UC_RESETDEV:
  815. tp->to_reset = 1;
  816. np->istat_sem = SEM;
  817. OUTB(np, nc_istat, SIGP|SEM);
  818. break;
  819. case UC_CLEARDEV:
  820. for (l = 0; l < SYM_CONF_MAX_LUN; l++) {
  821. struct sym_lcb *lp = sym_lp(tp, l);
  822. if (lp) lp->to_clear = 1;
  823. }
  824. np->istat_sem = SEM;
  825. OUTB(np, nc_istat, SIGP|SEM);
  826. break;
  827. case UC_SETFLAG:
  828. tp->usrflags = uc->data;
  829. break;
  830. }
  831. }
  832. break;
  833. }
  834. }
  835. static int skip_spaces(char *ptr, int len)
  836. {
  837. int cnt, c;
  838. for (cnt = len; cnt > 0 && (c = *ptr++) && isspace(c); cnt--);
  839. return (len - cnt);
  840. }
  841. static int get_int_arg(char *ptr, int len, u_long *pv)
  842. {
  843. char *end;
  844. *pv = simple_strtoul(ptr, &end, 10);
  845. return (end - ptr);
  846. }
  847. static int is_keyword(char *ptr, int len, char *verb)
  848. {
  849. int verb_len = strlen(verb);
  850. if (len >= verb_len && !memcmp(verb, ptr, verb_len))
  851. return verb_len;
  852. else
  853. return 0;
  854. }
  855. #define SKIP_SPACES(ptr, len) \
  856. if ((arg_len = skip_spaces(ptr, len)) < 1) \
  857. return -EINVAL; \
  858. ptr += arg_len; len -= arg_len;
  859. #define GET_INT_ARG(ptr, len, v) \
  860. if (!(arg_len = get_int_arg(ptr, len, &(v)))) \
  861. return -EINVAL; \
  862. ptr += arg_len; len -= arg_len;
  863. /*
  864. * Parse a control command
  865. */
  866. static int sym_user_command(struct Scsi_Host *shost, char *buffer, int length)
  867. {
  868. struct sym_hcb *np = sym_get_hcb(shost);
  869. char *ptr = buffer;
  870. int len = length;
  871. struct sym_usrcmd cmd, *uc = &cmd;
  872. int arg_len;
  873. u_long target;
  874. memset(uc, 0, sizeof(*uc));
  875. if (len > 0 && ptr[len-1] == '\n')
  876. --len;
  877. if ((arg_len = is_keyword(ptr, len, "setsync")) != 0)
  878. uc->cmd = UC_SETSYNC;
  879. else if ((arg_len = is_keyword(ptr, len, "settags")) != 0)
  880. uc->cmd = UC_SETTAGS;
  881. else if ((arg_len = is_keyword(ptr, len, "setverbose")) != 0)
  882. uc->cmd = UC_SETVERBOSE;
  883. else if ((arg_len = is_keyword(ptr, len, "setwide")) != 0)
  884. uc->cmd = UC_SETWIDE;
  885. #ifdef SYM_LINUX_DEBUG_CONTROL_SUPPORT
  886. else if ((arg_len = is_keyword(ptr, len, "setdebug")) != 0)
  887. uc->cmd = UC_SETDEBUG;
  888. #endif
  889. else if ((arg_len = is_keyword(ptr, len, "setflag")) != 0)
  890. uc->cmd = UC_SETFLAG;
  891. else if ((arg_len = is_keyword(ptr, len, "resetdev")) != 0)
  892. uc->cmd = UC_RESETDEV;
  893. else if ((arg_len = is_keyword(ptr, len, "cleardev")) != 0)
  894. uc->cmd = UC_CLEARDEV;
  895. else
  896. arg_len = 0;
  897. #ifdef DEBUG_PROC_INFO
  898. printk("sym_user_command: arg_len=%d, cmd=%ld\n", arg_len, uc->cmd);
  899. #endif
  900. if (!arg_len)
  901. return -EINVAL;
  902. ptr += arg_len; len -= arg_len;
  903. switch(uc->cmd) {
  904. case UC_SETSYNC:
  905. case UC_SETTAGS:
  906. case UC_SETWIDE:
  907. case UC_SETFLAG:
  908. case UC_RESETDEV:
  909. case UC_CLEARDEV:
  910. SKIP_SPACES(ptr, len);
  911. if ((arg_len = is_keyword(ptr, len, "all")) != 0) {
  912. ptr += arg_len; len -= arg_len;
  913. uc->target = ~0;
  914. } else {
  915. GET_INT_ARG(ptr, len, target);
  916. uc->target = (1<<target);
  917. #ifdef DEBUG_PROC_INFO
  918. printk("sym_user_command: target=%ld\n", target);
  919. #endif
  920. }
  921. break;
  922. }
  923. switch(uc->cmd) {
  924. case UC_SETVERBOSE:
  925. case UC_SETSYNC:
  926. case UC_SETTAGS:
  927. case UC_SETWIDE:
  928. SKIP_SPACES(ptr, len);
  929. GET_INT_ARG(ptr, len, uc->data);
  930. #ifdef DEBUG_PROC_INFO
  931. printk("sym_user_command: data=%ld\n", uc->data);
  932. #endif
  933. break;
  934. #ifdef SYM_LINUX_DEBUG_CONTROL_SUPPORT
  935. case UC_SETDEBUG:
  936. while (len > 0) {
  937. SKIP_SPACES(ptr, len);
  938. if ((arg_len = is_keyword(ptr, len, "alloc")))
  939. uc->data |= DEBUG_ALLOC;
  940. else if ((arg_len = is_keyword(ptr, len, "phase")))
  941. uc->data |= DEBUG_PHASE;
  942. else if ((arg_len = is_keyword(ptr, len, "queue")))
  943. uc->data |= DEBUG_QUEUE;
  944. else if ((arg_len = is_keyword(ptr, len, "result")))
  945. uc->data |= DEBUG_RESULT;
  946. else if ((arg_len = is_keyword(ptr, len, "scatter")))
  947. uc->data |= DEBUG_SCATTER;
  948. else if ((arg_len = is_keyword(ptr, len, "script")))
  949. uc->data |= DEBUG_SCRIPT;
  950. else if ((arg_len = is_keyword(ptr, len, "tiny")))
  951. uc->data |= DEBUG_TINY;
  952. else if ((arg_len = is_keyword(ptr, len, "timing")))
  953. uc->data |= DEBUG_TIMING;
  954. else if ((arg_len = is_keyword(ptr, len, "nego")))
  955. uc->data |= DEBUG_NEGO;
  956. else if ((arg_len = is_keyword(ptr, len, "tags")))
  957. uc->data |= DEBUG_TAGS;
  958. else if ((arg_len = is_keyword(ptr, len, "pointer")))
  959. uc->data |= DEBUG_POINTER;
  960. else
  961. return -EINVAL;
  962. ptr += arg_len; len -= arg_len;
  963. }
  964. #ifdef DEBUG_PROC_INFO
  965. printk("sym_user_command: data=%ld\n", uc->data);
  966. #endif
  967. break;
  968. #endif /* SYM_LINUX_DEBUG_CONTROL_SUPPORT */
  969. case UC_SETFLAG:
  970. while (len > 0) {
  971. SKIP_SPACES(ptr, len);
  972. if ((arg_len = is_keyword(ptr, len, "no_disc")))
  973. uc->data &= ~SYM_DISC_ENABLED;
  974. else
  975. return -EINVAL;
  976. ptr += arg_len; len -= arg_len;
  977. }
  978. break;
  979. default:
  980. break;
  981. }
  982. if (len)
  983. return -EINVAL;
  984. else {
  985. unsigned long flags;
  986. spin_lock_irqsave(shost->host_lock, flags);
  987. sym_exec_user_command(np, uc);
  988. spin_unlock_irqrestore(shost->host_lock, flags);
  989. }
  990. return length;
  991. }
  992. #endif /* SYM_LINUX_USER_COMMAND_SUPPORT */
  993. #ifdef SYM_LINUX_USER_INFO_SUPPORT
  994. /*
  995. * Informations through the proc file system.
  996. */
  997. struct info_str {
  998. char *buffer;
  999. int length;
  1000. int offset;
  1001. int pos;
  1002. };
  1003. static void copy_mem_info(struct info_str *info, char *data, int len)
  1004. {
  1005. if (info->pos + len > info->length)
  1006. len = info->length - info->pos;
  1007. if (info->pos + len < info->offset) {
  1008. info->pos += len;
  1009. return;
  1010. }
  1011. if (info->pos < info->offset) {
  1012. data += (info->offset - info->pos);
  1013. len -= (info->offset - info->pos);
  1014. }
  1015. if (len > 0) {
  1016. memcpy(info->buffer + info->pos, data, len);
  1017. info->pos += len;
  1018. }
  1019. }
  1020. static int copy_info(struct info_str *info, char *fmt, ...)
  1021. {
  1022. va_list args;
  1023. char buf[81];
  1024. int len;
  1025. va_start(args, fmt);
  1026. len = vsprintf(buf, fmt, args);
  1027. va_end(args);
  1028. copy_mem_info(info, buf, len);
  1029. return len;
  1030. }
  1031. /*
  1032. * Copy formatted information into the input buffer.
  1033. */
  1034. static int sym_host_info(struct Scsi_Host *shost, char *ptr, off_t offset, int len)
  1035. {
  1036. struct sym_data *sym_data = shost_priv(shost);
  1037. struct pci_dev *pdev = sym_data->pdev;
  1038. struct sym_hcb *np = sym_data->ncb;
  1039. struct info_str info;
  1040. info.buffer = ptr;
  1041. info.length = len;
  1042. info.offset = offset;
  1043. info.pos = 0;
  1044. copy_info(&info, "Chip " NAME53C "%s, device id 0x%x, "
  1045. "revision id 0x%x\n", np->s.chip_name,
  1046. pdev->device, pdev->revision);
  1047. copy_info(&info, "At PCI address %s, IRQ %u\n",
  1048. pci_name(pdev), pdev->irq);
  1049. copy_info(&info, "Min. period factor %d, %s SCSI BUS%s\n",
  1050. (int) (np->minsync_dt ? np->minsync_dt : np->minsync),
  1051. np->maxwide ? "Wide" : "Narrow",
  1052. np->minsync_dt ? ", DT capable" : "");
  1053. copy_info(&info, "Max. started commands %d, "
  1054. "max. commands per LUN %d\n",
  1055. SYM_CONF_MAX_START, SYM_CONF_MAX_TAG);
  1056. return info.pos > info.offset? info.pos - info.offset : 0;
  1057. }
  1058. #endif /* SYM_LINUX_USER_INFO_SUPPORT */
  1059. /*
  1060. * Entry point of the scsi proc fs of the driver.
  1061. * - func = 0 means read (returns adapter infos)
  1062. * - func = 1 means write (not yet merget from sym53c8xx)
  1063. */
  1064. static int sym53c8xx_proc_info(struct Scsi_Host *shost, char *buffer,
  1065. char **start, off_t offset, int length, int func)
  1066. {
  1067. int retv;
  1068. if (func) {
  1069. #ifdef SYM_LINUX_USER_COMMAND_SUPPORT
  1070. retv = sym_user_command(shost, buffer, length);
  1071. #else
  1072. retv = -EINVAL;
  1073. #endif
  1074. } else {
  1075. if (start)
  1076. *start = buffer;
  1077. #ifdef SYM_LINUX_USER_INFO_SUPPORT
  1078. retv = sym_host_info(shost, buffer, offset, length);
  1079. #else
  1080. retv = -EINVAL;
  1081. #endif
  1082. }
  1083. return retv;
  1084. }
  1085. #endif /* SYM_LINUX_PROC_INFO_SUPPORT */
  1086. /*
  1087. * Free controller resources.
  1088. */
  1089. static void sym_free_resources(struct sym_hcb *np, struct pci_dev *pdev)
  1090. {
  1091. /*
  1092. * Free O/S specific resources.
  1093. */
  1094. if (pdev->irq)
  1095. free_irq(pdev->irq, np->s.host);
  1096. if (np->s.ioaddr)
  1097. pci_iounmap(pdev, np->s.ioaddr);
  1098. if (np->s.ramaddr)
  1099. pci_iounmap(pdev, np->s.ramaddr);
  1100. /*
  1101. * Free O/S independent resources.
  1102. */
  1103. sym_hcb_free(np);
  1104. sym_mfree_dma(np, sizeof(*np), "HCB");
  1105. }
  1106. /*
  1107. * Host attach and initialisations.
  1108. *
  1109. * Allocate host data and ncb structure.
  1110. * Remap MMIO region.
  1111. * Do chip initialization.
  1112. * If all is OK, install interrupt handling and
  1113. * start the timer daemon.
  1114. */
  1115. static struct Scsi_Host * __devinit sym_attach(struct scsi_host_template *tpnt,
  1116. int unit, struct sym_device *dev)
  1117. {
  1118. struct sym_data *sym_data;
  1119. struct sym_hcb *np = NULL;
  1120. struct Scsi_Host *shost;
  1121. struct pci_dev *pdev = dev->pdev;
  1122. unsigned long flags;
  1123. struct sym_fw *fw;
  1124. printk(KERN_INFO "sym%d: <%s> rev 0x%x at pci %s irq %u\n",
  1125. unit, dev->chip.name, pdev->revision, pci_name(pdev),
  1126. pdev->irq);
  1127. /*
  1128. * Get the firmware for this chip.
  1129. */
  1130. fw = sym_find_firmware(&dev->chip);
  1131. if (!fw)
  1132. return NULL;
  1133. shost = scsi_host_alloc(tpnt, sizeof(*sym_data));
  1134. if (!shost)
  1135. return NULL;
  1136. sym_data = shost_priv(shost);
  1137. /*
  1138. * Allocate immediately the host control block,
  1139. * since we are only expecting to succeed. :)
  1140. * We keep track in the HCB of all the resources that
  1141. * are to be released on error.
  1142. */
  1143. np = __sym_calloc_dma(&pdev->dev, sizeof(*np), "HCB");
  1144. if (!np)
  1145. goto attach_failed;
  1146. np->bus_dmat = &pdev->dev; /* Result in 1 DMA pool per HBA */
  1147. sym_data->ncb = np;
  1148. sym_data->pdev = pdev;
  1149. np->s.host = shost;
  1150. pci_set_drvdata(pdev, shost);
  1151. /*
  1152. * Copy some useful infos to the HCB.
  1153. */
  1154. np->hcb_ba = vtobus(np);
  1155. np->verbose = sym_driver_setup.verbose;
  1156. np->s.unit = unit;
  1157. np->features = dev->chip.features;
  1158. np->clock_divn = dev->chip.nr_divisor;
  1159. np->maxoffs = dev->chip.offset_max;
  1160. np->maxburst = dev->chip.burst_max;
  1161. np->myaddr = dev->host_id;
  1162. /*
  1163. * Edit its name.
  1164. */
  1165. strlcpy(np->s.chip_name, dev->chip.name, sizeof(np->s.chip_name));
  1166. sprintf(np->s.inst_name, "sym%d", np->s.unit);
  1167. if ((SYM_CONF_DMA_ADDRESSING_MODE > 0) && (np->features & FE_DAC) &&
  1168. !pci_set_dma_mask(pdev, DMA_DAC_MASK)) {
  1169. set_dac(np);
  1170. } else if (pci_set_dma_mask(pdev, DMA_32BIT_MASK)) {
  1171. printf_warning("%s: No suitable DMA available\n", sym_name(np));
  1172. goto attach_failed;
  1173. }
  1174. /*
  1175. * Try to map the controller chip to
  1176. * virtual and physical memory.
  1177. */
  1178. np->mmio_ba = (u32)dev->mmio_base;
  1179. np->s.ioaddr = dev->s.ioaddr;
  1180. np->s.ramaddr = dev->s.ramaddr;
  1181. /*
  1182. * Map on-chip RAM if present and supported.
  1183. */
  1184. if (!(np->features & FE_RAM))
  1185. dev->ram_base = 0;
  1186. if (dev->ram_base)
  1187. np->ram_ba = (u32)dev->ram_base;
  1188. if (sym_hcb_attach(shost, fw, dev->nvram))
  1189. goto attach_failed;
  1190. /*
  1191. * Install the interrupt handler.
  1192. * If we synchonize the C code with SCRIPTS on interrupt,
  1193. * we do not want to share the INTR line at all.
  1194. */
  1195. if (request_irq(pdev->irq, sym53c8xx_intr, IRQF_SHARED, NAME53C8XX,
  1196. shost)) {
  1197. printf_err("%s: request irq %u failure\n",
  1198. sym_name(np), pdev->irq);
  1199. goto attach_failed;
  1200. }
  1201. /*
  1202. * After SCSI devices have been opened, we cannot
  1203. * reset the bus safely, so we do it here.
  1204. */
  1205. spin_lock_irqsave(shost->host_lock, flags);
  1206. if (sym_reset_scsi_bus(np, 0))
  1207. goto reset_failed;
  1208. /*
  1209. * Start the SCRIPTS.
  1210. */
  1211. sym_start_up(shost, 1);
  1212. /*
  1213. * Start the timer daemon
  1214. */
  1215. init_timer(&np->s.timer);
  1216. np->s.timer.data = (unsigned long) np;
  1217. np->s.timer.function = sym53c8xx_timer;
  1218. np->s.lasttime=0;
  1219. sym_timer (np);
  1220. /*
  1221. * Fill Linux host instance structure
  1222. * and return success.
  1223. */
  1224. shost->max_channel = 0;
  1225. shost->this_id = np->myaddr;
  1226. shost->max_id = np->maxwide ? 16 : 8;
  1227. shost->max_lun = SYM_CONF_MAX_LUN;
  1228. shost->unique_id = pci_resource_start(pdev, 0);
  1229. shost->cmd_per_lun = SYM_CONF_MAX_TAG;
  1230. shost->can_queue = (SYM_CONF_MAX_START-2);
  1231. shost->sg_tablesize = SYM_CONF_MAX_SG;
  1232. shost->max_cmd_len = 16;
  1233. BUG_ON(sym2_transport_template == NULL);
  1234. shost->transportt = sym2_transport_template;
  1235. /* 53c896 rev 1 errata: DMA may not cross 16MB boundary */
  1236. if (pdev->device == PCI_DEVICE_ID_NCR_53C896 && pdev->revision < 2)
  1237. shost->dma_boundary = 0xFFFFFF;
  1238. spin_unlock_irqrestore(shost->host_lock, flags);
  1239. return shost;
  1240. reset_failed:
  1241. printf_err("%s: FATAL ERROR: CHECK SCSI BUS - CABLES, "
  1242. "TERMINATION, DEVICE POWER etc.!\n", sym_name(np));
  1243. spin_unlock_irqrestore(shost->host_lock, flags);
  1244. attach_failed:
  1245. if (!shost)
  1246. return NULL;
  1247. printf_info("%s: giving up ...\n", sym_name(np));
  1248. if (np)
  1249. sym_free_resources(np, pdev);
  1250. scsi_host_put(shost);
  1251. return NULL;
  1252. }
  1253. /*
  1254. * Detect and try to read SYMBIOS and TEKRAM NVRAM.
  1255. */
  1256. #if SYM_CONF_NVRAM_SUPPORT
  1257. static void __devinit sym_get_nvram(struct sym_device *devp, struct sym_nvram *nvp)
  1258. {
  1259. devp->nvram = nvp;
  1260. nvp->type = 0;
  1261. sym_read_nvram(devp, nvp);
  1262. }
  1263. #else
  1264. static inline void sym_get_nvram(struct sym_device *devp, struct sym_nvram *nvp)
  1265. {
  1266. }
  1267. #endif /* SYM_CONF_NVRAM_SUPPORT */
  1268. static int __devinit sym_check_supported(struct sym_device *device)
  1269. {
  1270. struct sym_chip *chip;
  1271. struct pci_dev *pdev = device->pdev;
  1272. unsigned long io_port = pci_resource_start(pdev, 0);
  1273. int i;
  1274. /*
  1275. * If user excluded this chip, do not initialize it.
  1276. * I hate this code so much. Must kill it.
  1277. */
  1278. if (io_port) {
  1279. for (i = 0 ; i < 8 ; i++) {
  1280. if (sym_driver_setup.excludes[i] == io_port)
  1281. return -ENODEV;
  1282. }
  1283. }
  1284. /*
  1285. * Check if the chip is supported. Then copy the chip description
  1286. * to our device structure so we can make it match the actual device
  1287. * and options.
  1288. */
  1289. chip = sym_lookup_chip_table(pdev->device, pdev->revision);
  1290. if (!chip) {
  1291. dev_info(&pdev->dev, "device not supported\n");
  1292. return -ENODEV;
  1293. }
  1294. memcpy(&device->chip, chip, sizeof(device->chip));
  1295. return 0;
  1296. }
  1297. /*
  1298. * Ignore Symbios chips controlled by various RAID controllers.
  1299. * These controllers set value 0x52414944 at RAM end - 16.
  1300. */
  1301. static int __devinit sym_check_raid(struct sym_device *device)
  1302. {
  1303. unsigned int ram_size, ram_val;
  1304. if (!device->s.ramaddr)
  1305. return 0;
  1306. if (device->chip.features & FE_RAM8K)
  1307. ram_size = 8192;
  1308. else
  1309. ram_size = 4096;
  1310. ram_val = readl(device->s.ramaddr + ram_size - 16);
  1311. if (ram_val != 0x52414944)
  1312. return 0;
  1313. dev_info(&device->pdev->dev,
  1314. "not initializing, driven by RAID controller.\n");
  1315. return -ENODEV;
  1316. }
  1317. static int __devinit sym_set_workarounds(struct sym_device *device)
  1318. {
  1319. struct sym_chip *chip = &device->chip;
  1320. struct pci_dev *pdev = device->pdev;
  1321. u_short status_reg;
  1322. /*
  1323. * (ITEM 12 of a DEL about the 896 I haven't yet).
  1324. * We must ensure the chip will use WRITE AND INVALIDATE.
  1325. * The revision number limit is for now arbitrary.
  1326. */
  1327. if (pdev->device == PCI_DEVICE_ID_NCR_53C896 && pdev->revision < 0x4) {
  1328. chip->features |= (FE_WRIE | FE_CLSE);
  1329. }
  1330. /* If the chip can do Memory Write Invalidate, enable it */
  1331. if (chip->features & FE_WRIE) {
  1332. if (pci_set_mwi(pdev))
  1333. return -ENODEV;
  1334. }
  1335. /*
  1336. * Work around for errant bit in 895A. The 66Mhz
  1337. * capable bit is set erroneously. Clear this bit.
  1338. * (Item 1 DEL 533)
  1339. *
  1340. * Make sure Config space and Features agree.
  1341. *
  1342. * Recall: writes are not normal to status register -
  1343. * write a 1 to clear and a 0 to leave unchanged.
  1344. * Can only reset bits.
  1345. */
  1346. pci_read_config_word(pdev, PCI_STATUS, &status_reg);
  1347. if (chip->features & FE_66MHZ) {
  1348. if (!(status_reg & PCI_STATUS_66MHZ))
  1349. chip->features &= ~FE_66MHZ;
  1350. } else {
  1351. if (status_reg & PCI_STATUS_66MHZ) {
  1352. status_reg = PCI_STATUS_66MHZ;
  1353. pci_write_config_word(pdev, PCI_STATUS, status_reg);
  1354. pci_read_config_word(pdev, PCI_STATUS, &status_reg);
  1355. }
  1356. }
  1357. return 0;
  1358. }
  1359. /*
  1360. * Read and check the PCI configuration for any detected NCR
  1361. * boards and save data for attaching after all boards have
  1362. * been detected.
  1363. */
  1364. static void __devinit
  1365. sym_init_device(struct pci_dev *pdev, struct sym_device *device)
  1366. {
  1367. int i = 2;
  1368. struct pci_bus_region bus_addr;
  1369. device->host_id = SYM_SETUP_HOST_ID;
  1370. device->pdev = pdev;
  1371. pcibios_resource_to_bus(pdev, &bus_addr, &pdev->resource[1]);
  1372. device->mmio_base = bus_addr.start;
  1373. /*
  1374. * If the BAR is 64-bit, resource 2 will be occupied by the
  1375. * upper 32 bits
  1376. */
  1377. if (!pdev->resource[i].flags)
  1378. i++;
  1379. pcibios_resource_to_bus(pdev, &bus_addr, &pdev->resource[i]);
  1380. device->ram_base = bus_addr.start;
  1381. #ifdef CONFIG_SCSI_SYM53C8XX_MMIO
  1382. if (device->mmio_base)
  1383. device->s.ioaddr = pci_iomap(pdev, 1,
  1384. pci_resource_len(pdev, 1));
  1385. #endif
  1386. if (!device->s.ioaddr)
  1387. device->s.ioaddr = pci_iomap(pdev, 0,
  1388. pci_resource_len(pdev, 0));
  1389. if (device->ram_base)
  1390. device->s.ramaddr = pci_iomap(pdev, i,
  1391. pci_resource_len(pdev, i));
  1392. }
  1393. /*
  1394. * The NCR PQS and PDS cards are constructed as a DEC bridge
  1395. * behind which sits a proprietary NCR memory controller and
  1396. * either four or two 53c875s as separate devices. We can tell
  1397. * if an 875 is part of a PQS/PDS or not since if it is, it will
  1398. * be on the same bus as the memory controller. In its usual
  1399. * mode of operation, the 875s are slaved to the memory
  1400. * controller for all transfers. To operate with the Linux
  1401. * driver, the memory controller is disabled and the 875s
  1402. * freed to function independently. The only wrinkle is that
  1403. * the preset SCSI ID (which may be zero) must be read in from
  1404. * a special configuration space register of the 875.
  1405. */
  1406. static void sym_config_pqs(struct pci_dev *pdev, struct sym_device *sym_dev)
  1407. {
  1408. int slot;
  1409. u8 tmp;
  1410. for (slot = 0; slot < 256; slot++) {
  1411. struct pci_dev *memc = pci_get_slot(pdev->bus, slot);
  1412. if (!memc || memc->vendor != 0x101a || memc->device == 0x0009) {
  1413. pci_dev_put(memc);
  1414. continue;
  1415. }
  1416. /* bit 1: allow individual 875 configuration */
  1417. pci_read_config_byte(memc, 0x44, &tmp);
  1418. if ((tmp & 0x2) == 0) {
  1419. tmp |= 0x2;
  1420. pci_write_config_byte(memc, 0x44, tmp);
  1421. }
  1422. /* bit 2: drive individual 875 interrupts to the bus */
  1423. pci_read_config_byte(memc, 0x45, &tmp);
  1424. if ((tmp & 0x4) == 0) {
  1425. tmp |= 0x4;
  1426. pci_write_config_byte(memc, 0x45, tmp);
  1427. }
  1428. pci_dev_put(memc);
  1429. break;
  1430. }
  1431. pci_read_config_byte(pdev, 0x84, &tmp);
  1432. sym_dev->host_id = tmp;
  1433. }
  1434. /*
  1435. * Called before unloading the module.
  1436. * Detach the host.
  1437. * We have to free resources and halt the NCR chip.
  1438. */
  1439. static int sym_detach(struct Scsi_Host *shost, struct pci_dev *pdev)
  1440. {
  1441. struct sym_hcb *np = sym_get_hcb(shost);
  1442. printk("%s: detaching ...\n", sym_name(np));
  1443. del_timer_sync(&np->s.timer);
  1444. /*
  1445. * Reset NCR chip.
  1446. * We should use sym_soft_reset(), but we don't want to do
  1447. * so, since we may not be safe if interrupts occur.
  1448. */
  1449. printk("%s: resetting chip\n", sym_name(np));
  1450. OUTB(np, nc_istat, SRST);
  1451. INB(np, nc_mbox1);
  1452. udelay(10);
  1453. OUTB(np, nc_istat, 0);
  1454. sym_free_resources(np, pdev);
  1455. return 1;
  1456. }
  1457. /*
  1458. * Driver host template.
  1459. */
  1460. static struct scsi_host_template sym2_template = {
  1461. .module = THIS_MODULE,
  1462. .name = "sym53c8xx",
  1463. .info = sym53c8xx_info,
  1464. .queuecommand = sym53c8xx_queue_command,
  1465. .slave_alloc = sym53c8xx_slave_alloc,
  1466. .slave_configure = sym53c8xx_slave_configure,
  1467. .slave_destroy = sym53c8xx_slave_destroy,
  1468. .eh_abort_handler = sym53c8xx_eh_abort_handler,
  1469. .eh_device_reset_handler = sym53c8xx_eh_device_reset_handler,
  1470. .eh_bus_reset_handler = sym53c8xx_eh_bus_reset_handler,
  1471. .eh_host_reset_handler = sym53c8xx_eh_host_reset_handler,
  1472. .this_id = 7,
  1473. .use_clustering = ENABLE_CLUSTERING,
  1474. .max_sectors = 0xFFFF,
  1475. #ifdef SYM_LINUX_PROC_INFO_SUPPORT
  1476. .proc_info = sym53c8xx_proc_info,
  1477. .proc_name = NAME53C8XX,
  1478. #endif
  1479. };
  1480. static int attach_count;
  1481. static int __devinit sym2_probe(struct pci_dev *pdev,
  1482. const struct pci_device_id *ent)
  1483. {
  1484. struct sym_device sym_dev;
  1485. struct sym_nvram nvram;
  1486. struct Scsi_Host *shost;
  1487. memset(&sym_dev, 0, sizeof(sym_dev));
  1488. memset(&nvram, 0, sizeof(nvram));
  1489. if (pci_enable_device(pdev))
  1490. goto leave;
  1491. pci_set_master(pdev);
  1492. if (pci_request_regions(pdev, NAME53C8XX))
  1493. goto disable;
  1494. sym_init_device(pdev, &sym_dev);
  1495. if (sym_check_supported(&sym_dev))
  1496. goto free;
  1497. if (sym_check_raid(&sym_dev))
  1498. goto leave; /* Don't disable the device */
  1499. if (sym_set_workarounds(&sym_dev))
  1500. goto free;
  1501. sym_config_pqs(pdev, &sym_dev);
  1502. sym_get_nvram(&sym_dev, &nvram);
  1503. shost = sym_attach(&sym2_template, attach_count, &sym_dev);
  1504. if (!shost)
  1505. goto free;
  1506. if (scsi_add_host(shost, &pdev->dev))
  1507. goto detach;
  1508. scsi_scan_host(shost);
  1509. attach_count++;
  1510. return 0;
  1511. detach:
  1512. sym_detach(pci_get_drvdata(pdev), pdev);
  1513. free:
  1514. pci_release_regions(pdev);
  1515. disable:
  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. scsi_host_put(shost);
  1525. sym_detach(shost, pdev);
  1526. pci_release_regions(pdev);
  1527. pci_disable_device(pdev);
  1528. attach_count--;
  1529. }
  1530. /**
  1531. * sym2_io_error_detected() - called when PCI error is detected
  1532. * @pdev: pointer to PCI device
  1533. * @state: current state of the PCI slot
  1534. */
  1535. static pci_ers_result_t sym2_io_error_detected(struct pci_dev *pdev,
  1536. enum pci_channel_state state)
  1537. {
  1538. /* If slot is permanently frozen, turn everything off */
  1539. if (state == pci_channel_io_perm_failure) {
  1540. sym2_remove(pdev);
  1541. return PCI_ERS_RESULT_DISCONNECT;
  1542. }
  1543. disable_irq(pdev->irq);
  1544. pci_disable_device(pdev);
  1545. /* Request that MMIO be enabled, so register dump can be taken. */
  1546. return PCI_ERS_RESULT_CAN_RECOVER;
  1547. }
  1548. /**
  1549. * sym2_io_slot_dump - Enable MMIO and dump debug registers
  1550. * @pdev: pointer to PCI device
  1551. */
  1552. static pci_ers_result_t sym2_io_slot_dump(struct pci_dev *pdev)
  1553. {
  1554. struct Scsi_Host *shost = pci_get_drvdata(pdev);
  1555. sym_dump_registers(shost);
  1556. /* Request a slot reset. */
  1557. return PCI_ERS_RESULT_NEED_RESET;
  1558. }
  1559. /**
  1560. * sym2_reset_workarounds - hardware-specific work-arounds
  1561. *
  1562. * This routine is similar to sym_set_workarounds(), except
  1563. * that, at this point, we already know that the device was
  1564. * succesfully intialized at least once before, and so most
  1565. * of the steps taken there are un-needed here.
  1566. */
  1567. static void sym2_reset_workarounds(struct pci_dev *pdev)
  1568. {
  1569. u_short status_reg;
  1570. struct sym_chip *chip;
  1571. chip = sym_lookup_chip_table(pdev->device, pdev->revision);
  1572. /* Work around for errant bit in 895A, in a fashion
  1573. * similar to what is done in sym_set_workarounds().
  1574. */
  1575. pci_read_config_word(pdev, PCI_STATUS, &status_reg);
  1576. if (!(chip->features & FE_66MHZ) && (status_reg & PCI_STATUS_66MHZ)) {
  1577. status_reg = PCI_STATUS_66MHZ;
  1578. pci_write_config_word(pdev, PCI_STATUS, status_reg);
  1579. pci_read_config_word(pdev, PCI_STATUS, &status_reg);
  1580. }
  1581. }
  1582. /**
  1583. * sym2_io_slot_reset() - called when the pci bus has been reset.
  1584. * @pdev: pointer to PCI device
  1585. *
  1586. * Restart the card from scratch.
  1587. */
  1588. static pci_ers_result_t sym2_io_slot_reset(struct pci_dev *pdev)
  1589. {
  1590. struct Scsi_Host *shost = pci_get_drvdata(pdev);
  1591. struct sym_hcb *np = sym_get_hcb(shost);
  1592. printk(KERN_INFO "%s: recovering from a PCI slot reset\n",
  1593. sym_name(np));
  1594. if (pci_enable_device(pdev)) {
  1595. printk(KERN_ERR "%s: Unable to enable after PCI reset\n",
  1596. sym_name(np));
  1597. return PCI_ERS_RESULT_DISCONNECT;
  1598. }
  1599. pci_set_master(pdev);
  1600. enable_irq(pdev->irq);
  1601. /* If the chip can do Memory Write Invalidate, enable it */
  1602. if (np->features & FE_WRIE) {
  1603. if (pci_set_mwi(pdev))
  1604. return PCI_ERS_RESULT_DISCONNECT;
  1605. }
  1606. /* Perform work-arounds, analogous to sym_set_workarounds() */
  1607. sym2_reset_workarounds(pdev);
  1608. /* Perform host reset only on one instance of the card */
  1609. if (PCI_FUNC(pdev->devfn) == 0) {
  1610. if (sym_reset_scsi_bus(np, 0)) {
  1611. printk(KERN_ERR "%s: Unable to reset scsi host\n",
  1612. sym_name(np));
  1613. return PCI_ERS_RESULT_DISCONNECT;
  1614. }
  1615. sym_start_up(shost, 1);
  1616. }
  1617. return PCI_ERS_RESULT_RECOVERED;
  1618. }
  1619. /**
  1620. * sym2_io_resume() - resume normal ops after PCI reset
  1621. * @pdev: pointer to PCI device
  1622. *
  1623. * Called when the error recovery driver tells us that its
  1624. * OK to resume normal operation. Use completion to allow
  1625. * halted scsi ops to resume.
  1626. */
  1627. static void sym2_io_resume(struct pci_dev *pdev)
  1628. {
  1629. struct Scsi_Host *shost = pci_get_drvdata(pdev);
  1630. struct sym_data *sym_data = shost_priv(shost);
  1631. spin_lock_irq(shost->host_lock);
  1632. if (sym_data->io_reset)
  1633. complete_all(sym_data->io_reset);
  1634. spin_unlock_irq(shost->host_lock);
  1635. }
  1636. static void sym2_get_signalling(struct Scsi_Host *shost)
  1637. {
  1638. struct sym_hcb *np = sym_get_hcb(shost);
  1639. enum spi_signal_type type;
  1640. switch (np->scsi_mode) {
  1641. case SMODE_SE:
  1642. type = SPI_SIGNAL_SE;
  1643. break;
  1644. case SMODE_LVD:
  1645. type = SPI_SIGNAL_LVD;
  1646. break;
  1647. case SMODE_HVD:
  1648. type = SPI_SIGNAL_HVD;
  1649. break;
  1650. default:
  1651. type = SPI_SIGNAL_UNKNOWN;
  1652. break;
  1653. }
  1654. spi_signalling(shost) = type;
  1655. }
  1656. static void sym2_set_offset(struct scsi_target *starget, int offset)
  1657. {
  1658. struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
  1659. struct sym_hcb *np = sym_get_hcb(shost);
  1660. struct sym_tcb *tp = &np->target[starget->id];
  1661. tp->tgoal.offset = offset;
  1662. tp->tgoal.check_nego = 1;
  1663. }
  1664. static void sym2_set_period(struct scsi_target *starget, int period)
  1665. {
  1666. struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
  1667. struct sym_hcb *np = sym_get_hcb(shost);
  1668. struct sym_tcb *tp = &np->target[starget->id];
  1669. /* have to have DT for these transfers, but DT will also
  1670. * set width, so check that this is allowed */
  1671. if (period <= np->minsync && spi_width(starget))
  1672. tp->tgoal.dt = 1;
  1673. tp->tgoal.period = period;
  1674. tp->tgoal.check_nego = 1;
  1675. }
  1676. static void sym2_set_width(struct scsi_target *starget, int width)
  1677. {
  1678. struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
  1679. struct sym_hcb *np = sym_get_hcb(shost);
  1680. struct sym_tcb *tp = &np->target[starget->id];
  1681. /* It is illegal to have DT set on narrow transfers. If DT is
  1682. * clear, we must also clear IU and QAS. */
  1683. if (width == 0)
  1684. tp->tgoal.iu = tp->tgoal.dt = tp->tgoal.qas = 0;
  1685. tp->tgoal.width = width;
  1686. tp->tgoal.check_nego = 1;
  1687. }
  1688. static void sym2_set_dt(struct scsi_target *starget, int dt)
  1689. {
  1690. struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
  1691. struct sym_hcb *np = sym_get_hcb(shost);
  1692. struct sym_tcb *tp = &np->target[starget->id];
  1693. /* We must clear QAS and IU if DT is clear */
  1694. if (dt)
  1695. tp->tgoal.dt = 1;
  1696. else
  1697. tp->tgoal.iu = tp->tgoal.dt = tp->tgoal.qas = 0;
  1698. tp->tgoal.check_nego = 1;
  1699. }
  1700. #if 0
  1701. static void sym2_set_iu(struct scsi_target *starget, int iu)
  1702. {
  1703. struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
  1704. struct sym_hcb *np = sym_get_hcb(shost);
  1705. struct sym_tcb *tp = &np->target[starget->id];
  1706. if (iu)
  1707. tp->tgoal.iu = tp->tgoal.dt = 1;
  1708. else
  1709. tp->tgoal.iu = 0;
  1710. tp->tgoal.check_nego = 1;
  1711. }
  1712. static void sym2_set_qas(struct scsi_target *starget, int qas)
  1713. {
  1714. struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
  1715. struct sym_hcb *np = sym_get_hcb(shost);
  1716. struct sym_tcb *tp = &np->target[starget->id];
  1717. if (qas)
  1718. tp->tgoal.dt = tp->tgoal.qas = 1;
  1719. else
  1720. tp->tgoal.qas = 0;
  1721. tp->tgoal.check_nego = 1;
  1722. }
  1723. #endif
  1724. static struct spi_function_template sym2_transport_functions = {
  1725. .set_offset = sym2_set_offset,
  1726. .show_offset = 1,
  1727. .set_period = sym2_set_period,
  1728. .show_period = 1,
  1729. .set_width = sym2_set_width,
  1730. .show_width = 1,
  1731. .set_dt = sym2_set_dt,
  1732. .show_dt = 1,
  1733. #if 0
  1734. .set_iu = sym2_set_iu,
  1735. .show_iu = 1,
  1736. .set_qas = sym2_set_qas,
  1737. .show_qas = 1,
  1738. #endif
  1739. .get_signalling = sym2_get_signalling,
  1740. };
  1741. static struct pci_device_id sym2_id_table[] __devinitdata = {
  1742. { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_NCR_53C810,
  1743. PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0UL },
  1744. { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_NCR_53C820,
  1745. PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0UL }, /* new */
  1746. { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_NCR_53C825,
  1747. PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0UL },
  1748. { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_NCR_53C815,
  1749. PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0UL },
  1750. { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_53C810AP,
  1751. PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0UL }, /* new */
  1752. { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_NCR_53C860,
  1753. PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0UL },
  1754. { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_53C1510,
  1755. PCI_ANY_ID, PCI_ANY_ID, PCI_CLASS_STORAGE_SCSI<<8, 0xffff00, 0UL },
  1756. { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_NCR_53C896,
  1757. PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0UL },
  1758. { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_NCR_53C895,
  1759. PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0UL },
  1760. { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_NCR_53C885,
  1761. PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0UL },
  1762. { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_NCR_53C875,
  1763. PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0UL },
  1764. { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_NCR_53C1510,
  1765. PCI_ANY_ID, PCI_ANY_ID, PCI_CLASS_STORAGE_SCSI<<8, 0xffff00, 0UL }, /* new */
  1766. { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_53C895A,
  1767. PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0UL },
  1768. { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_53C875A,
  1769. PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0UL },
  1770. { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_53C1010_33,
  1771. PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0UL },
  1772. { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_53C1010_66,
  1773. PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0UL },
  1774. { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_NCR_53C875J,
  1775. PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0UL },
  1776. { 0, }
  1777. };
  1778. MODULE_DEVICE_TABLE(pci, sym2_id_table);
  1779. static struct pci_error_handlers sym2_err_handler = {
  1780. .error_detected = sym2_io_error_detected,
  1781. .mmio_enabled = sym2_io_slot_dump,
  1782. .slot_reset = sym2_io_slot_reset,
  1783. .resume = sym2_io_resume,
  1784. };
  1785. static struct pci_driver sym2_driver = {
  1786. .name = NAME53C8XX,
  1787. .id_table = sym2_id_table,
  1788. .probe = sym2_probe,
  1789. .remove = sym2_remove,
  1790. .err_handler = &sym2_err_handler,
  1791. };
  1792. static int __init sym2_init(void)
  1793. {
  1794. int error;
  1795. sym2_setup_params();
  1796. sym2_transport_template = spi_attach_transport(&sym2_transport_functions);
  1797. if (!sym2_transport_template)
  1798. return -ENODEV;
  1799. error = pci_register_driver(&sym2_driver);
  1800. if (error)
  1801. spi_release_transport(sym2_transport_template);
  1802. return error;
  1803. }
  1804. static void __exit sym2_exit(void)
  1805. {
  1806. pci_unregister_driver(&sym2_driver);
  1807. spi_release_transport(sym2_transport_template);
  1808. }
  1809. module_init(sym2_init);
  1810. module_exit(sym2_exit);