sym_glue.c 54 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153
  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_adjust_queue_depth(sdev,
  723. sdev->tagged_supported ? MSG_SIMPLE_TAG : 0,
  724. depth_to_use);
  725. lp->s.scdev_depth = depth_to_use;
  726. sym_tune_dev_queuing(tp, sdev->lun, reqtags);
  727. if (!spi_initial_dv(sdev->sdev_target))
  728. spi_dv_device(sdev);
  729. return 0;
  730. }
  731. static void sym53c8xx_slave_destroy(struct scsi_device *sdev)
  732. {
  733. struct sym_hcb *np = sym_get_hcb(sdev->host);
  734. struct sym_tcb *tp = &np->target[sdev->id];
  735. struct sym_lcb *lp = sym_lp(tp, sdev->lun);
  736. unsigned long flags;
  737. spin_lock_irqsave(np->s.host->host_lock, flags);
  738. if (lp->busy_itlq || lp->busy_itl) {
  739. /*
  740. * This really shouldn't happen, but we can't return an error
  741. * so let's try to stop all on-going I/O.
  742. */
  743. starget_printk(KERN_WARNING, tp->starget,
  744. "Removing busy LCB (%d)\n", sdev->lun);
  745. sym_reset_scsi_bus(np, 1);
  746. }
  747. if (sym_free_lcb(np, sdev->id, sdev->lun) == 0) {
  748. /*
  749. * It was the last unit for this target.
  750. */
  751. tp->head.sval = 0;
  752. tp->head.wval = np->rv_scntl3;
  753. tp->head.uval = 0;
  754. tp->tgoal.check_nego = 1;
  755. tp->starget = NULL;
  756. }
  757. spin_unlock_irqrestore(np->s.host->host_lock, flags);
  758. }
  759. /*
  760. * Linux entry point for info() function
  761. */
  762. static const char *sym53c8xx_info (struct Scsi_Host *host)
  763. {
  764. return SYM_DRIVER_NAME;
  765. }
  766. #ifdef SYM_LINUX_PROC_INFO_SUPPORT
  767. /*
  768. * Proc file system stuff
  769. *
  770. * A read operation returns adapter information.
  771. * A write operation is a control command.
  772. * The string is parsed in the driver code and the command is passed
  773. * to the sym_usercmd() function.
  774. */
  775. #ifdef SYM_LINUX_USER_COMMAND_SUPPORT
  776. struct sym_usrcmd {
  777. u_long target;
  778. u_long lun;
  779. u_long data;
  780. u_long cmd;
  781. };
  782. #define UC_SETSYNC 10
  783. #define UC_SETTAGS 11
  784. #define UC_SETDEBUG 12
  785. #define UC_SETWIDE 14
  786. #define UC_SETFLAG 15
  787. #define UC_SETVERBOSE 17
  788. #define UC_RESETDEV 18
  789. #define UC_CLEARDEV 19
  790. static void sym_exec_user_command (struct sym_hcb *np, struct sym_usrcmd *uc)
  791. {
  792. struct sym_tcb *tp;
  793. int t, l;
  794. switch (uc->cmd) {
  795. case 0: return;
  796. #ifdef SYM_LINUX_DEBUG_CONTROL_SUPPORT
  797. case UC_SETDEBUG:
  798. sym_debug_flags = uc->data;
  799. break;
  800. #endif
  801. case UC_SETVERBOSE:
  802. np->verbose = uc->data;
  803. break;
  804. default:
  805. /*
  806. * We assume that other commands apply to targets.
  807. * This should always be the case and avoid the below
  808. * 4 lines to be repeated 6 times.
  809. */
  810. for (t = 0; t < SYM_CONF_MAX_TARGET; t++) {
  811. if (!((uc->target >> t) & 1))
  812. continue;
  813. tp = &np->target[t];
  814. if (!tp->nlcb)
  815. continue;
  816. switch (uc->cmd) {
  817. case UC_SETSYNC:
  818. if (!uc->data || uc->data >= 255) {
  819. tp->tgoal.iu = tp->tgoal.dt =
  820. tp->tgoal.qas = 0;
  821. tp->tgoal.offset = 0;
  822. } else if (uc->data <= 9 && np->minsync_dt) {
  823. if (uc->data < np->minsync_dt)
  824. uc->data = np->minsync_dt;
  825. tp->tgoal.iu = tp->tgoal.dt =
  826. tp->tgoal.qas = 1;
  827. tp->tgoal.width = 1;
  828. tp->tgoal.period = uc->data;
  829. tp->tgoal.offset = np->maxoffs_dt;
  830. } else {
  831. if (uc->data < np->minsync)
  832. uc->data = np->minsync;
  833. tp->tgoal.iu = tp->tgoal.dt =
  834. tp->tgoal.qas = 0;
  835. tp->tgoal.period = uc->data;
  836. tp->tgoal.offset = np->maxoffs;
  837. }
  838. tp->tgoal.check_nego = 1;
  839. break;
  840. case UC_SETWIDE:
  841. tp->tgoal.width = uc->data ? 1 : 0;
  842. tp->tgoal.check_nego = 1;
  843. break;
  844. case UC_SETTAGS:
  845. for (l = 0; l < SYM_CONF_MAX_LUN; l++)
  846. sym_tune_dev_queuing(tp, l, uc->data);
  847. break;
  848. case UC_RESETDEV:
  849. tp->to_reset = 1;
  850. np->istat_sem = SEM;
  851. OUTB(np, nc_istat, SIGP|SEM);
  852. break;
  853. case UC_CLEARDEV:
  854. for (l = 0; l < SYM_CONF_MAX_LUN; l++) {
  855. struct sym_lcb *lp = sym_lp(tp, l);
  856. if (lp) lp->to_clear = 1;
  857. }
  858. np->istat_sem = SEM;
  859. OUTB(np, nc_istat, SIGP|SEM);
  860. break;
  861. case UC_SETFLAG:
  862. tp->usrflags = uc->data;
  863. break;
  864. }
  865. }
  866. break;
  867. }
  868. }
  869. static int sym_skip_spaces(char *ptr, int len)
  870. {
  871. int cnt, c;
  872. for (cnt = len; cnt > 0 && (c = *ptr++) && isspace(c); cnt--);
  873. return (len - cnt);
  874. }
  875. static int get_int_arg(char *ptr, int len, u_long *pv)
  876. {
  877. char *end;
  878. *pv = simple_strtoul(ptr, &end, 10);
  879. return (end - ptr);
  880. }
  881. static int is_keyword(char *ptr, int len, char *verb)
  882. {
  883. int verb_len = strlen(verb);
  884. if (len >= verb_len && !memcmp(verb, ptr, verb_len))
  885. return verb_len;
  886. else
  887. return 0;
  888. }
  889. #define SKIP_SPACES(ptr, len) \
  890. if ((arg_len = sym_skip_spaces(ptr, len)) < 1) \
  891. return -EINVAL; \
  892. ptr += arg_len; len -= arg_len;
  893. #define GET_INT_ARG(ptr, len, v) \
  894. if (!(arg_len = get_int_arg(ptr, len, &(v)))) \
  895. return -EINVAL; \
  896. ptr += arg_len; len -= arg_len;
  897. /*
  898. * Parse a control command
  899. */
  900. static int sym_user_command(struct Scsi_Host *shost, char *buffer, int length)
  901. {
  902. struct sym_hcb *np = sym_get_hcb(shost);
  903. char *ptr = buffer;
  904. int len = length;
  905. struct sym_usrcmd cmd, *uc = &cmd;
  906. int arg_len;
  907. u_long target;
  908. memset(uc, 0, sizeof(*uc));
  909. if (len > 0 && ptr[len-1] == '\n')
  910. --len;
  911. if ((arg_len = is_keyword(ptr, len, "setsync")) != 0)
  912. uc->cmd = UC_SETSYNC;
  913. else if ((arg_len = is_keyword(ptr, len, "settags")) != 0)
  914. uc->cmd = UC_SETTAGS;
  915. else if ((arg_len = is_keyword(ptr, len, "setverbose")) != 0)
  916. uc->cmd = UC_SETVERBOSE;
  917. else if ((arg_len = is_keyword(ptr, len, "setwide")) != 0)
  918. uc->cmd = UC_SETWIDE;
  919. #ifdef SYM_LINUX_DEBUG_CONTROL_SUPPORT
  920. else if ((arg_len = is_keyword(ptr, len, "setdebug")) != 0)
  921. uc->cmd = UC_SETDEBUG;
  922. #endif
  923. else if ((arg_len = is_keyword(ptr, len, "setflag")) != 0)
  924. uc->cmd = UC_SETFLAG;
  925. else if ((arg_len = is_keyword(ptr, len, "resetdev")) != 0)
  926. uc->cmd = UC_RESETDEV;
  927. else if ((arg_len = is_keyword(ptr, len, "cleardev")) != 0)
  928. uc->cmd = UC_CLEARDEV;
  929. else
  930. arg_len = 0;
  931. #ifdef DEBUG_PROC_INFO
  932. printk("sym_user_command: arg_len=%d, cmd=%ld\n", arg_len, uc->cmd);
  933. #endif
  934. if (!arg_len)
  935. return -EINVAL;
  936. ptr += arg_len; len -= arg_len;
  937. switch(uc->cmd) {
  938. case UC_SETSYNC:
  939. case UC_SETTAGS:
  940. case UC_SETWIDE:
  941. case UC_SETFLAG:
  942. case UC_RESETDEV:
  943. case UC_CLEARDEV:
  944. SKIP_SPACES(ptr, len);
  945. if ((arg_len = is_keyword(ptr, len, "all")) != 0) {
  946. ptr += arg_len; len -= arg_len;
  947. uc->target = ~0;
  948. } else {
  949. GET_INT_ARG(ptr, len, target);
  950. uc->target = (1<<target);
  951. #ifdef DEBUG_PROC_INFO
  952. printk("sym_user_command: target=%ld\n", target);
  953. #endif
  954. }
  955. break;
  956. }
  957. switch(uc->cmd) {
  958. case UC_SETVERBOSE:
  959. case UC_SETSYNC:
  960. case UC_SETTAGS:
  961. case UC_SETWIDE:
  962. SKIP_SPACES(ptr, len);
  963. GET_INT_ARG(ptr, len, uc->data);
  964. #ifdef DEBUG_PROC_INFO
  965. printk("sym_user_command: data=%ld\n", uc->data);
  966. #endif
  967. break;
  968. #ifdef SYM_LINUX_DEBUG_CONTROL_SUPPORT
  969. case UC_SETDEBUG:
  970. while (len > 0) {
  971. SKIP_SPACES(ptr, len);
  972. if ((arg_len = is_keyword(ptr, len, "alloc")))
  973. uc->data |= DEBUG_ALLOC;
  974. else if ((arg_len = is_keyword(ptr, len, "phase")))
  975. uc->data |= DEBUG_PHASE;
  976. else if ((arg_len = is_keyword(ptr, len, "queue")))
  977. uc->data |= DEBUG_QUEUE;
  978. else if ((arg_len = is_keyword(ptr, len, "result")))
  979. uc->data |= DEBUG_RESULT;
  980. else if ((arg_len = is_keyword(ptr, len, "scatter")))
  981. uc->data |= DEBUG_SCATTER;
  982. else if ((arg_len = is_keyword(ptr, len, "script")))
  983. uc->data |= DEBUG_SCRIPT;
  984. else if ((arg_len = is_keyword(ptr, len, "tiny")))
  985. uc->data |= DEBUG_TINY;
  986. else if ((arg_len = is_keyword(ptr, len, "timing")))
  987. uc->data |= DEBUG_TIMING;
  988. else if ((arg_len = is_keyword(ptr, len, "nego")))
  989. uc->data |= DEBUG_NEGO;
  990. else if ((arg_len = is_keyword(ptr, len, "tags")))
  991. uc->data |= DEBUG_TAGS;
  992. else if ((arg_len = is_keyword(ptr, len, "pointer")))
  993. uc->data |= DEBUG_POINTER;
  994. else
  995. return -EINVAL;
  996. ptr += arg_len; len -= arg_len;
  997. }
  998. #ifdef DEBUG_PROC_INFO
  999. printk("sym_user_command: data=%ld\n", uc->data);
  1000. #endif
  1001. break;
  1002. #endif /* SYM_LINUX_DEBUG_CONTROL_SUPPORT */
  1003. case UC_SETFLAG:
  1004. while (len > 0) {
  1005. SKIP_SPACES(ptr, len);
  1006. if ((arg_len = is_keyword(ptr, len, "no_disc")))
  1007. uc->data &= ~SYM_DISC_ENABLED;
  1008. else
  1009. return -EINVAL;
  1010. ptr += arg_len; len -= arg_len;
  1011. }
  1012. break;
  1013. default:
  1014. break;
  1015. }
  1016. if (len)
  1017. return -EINVAL;
  1018. else {
  1019. unsigned long flags;
  1020. spin_lock_irqsave(shost->host_lock, flags);
  1021. sym_exec_user_command(np, uc);
  1022. spin_unlock_irqrestore(shost->host_lock, flags);
  1023. }
  1024. return length;
  1025. }
  1026. #endif /* SYM_LINUX_USER_COMMAND_SUPPORT */
  1027. #ifdef SYM_LINUX_USER_INFO_SUPPORT
  1028. /*
  1029. * Informations through the proc file system.
  1030. */
  1031. struct info_str {
  1032. char *buffer;
  1033. int length;
  1034. int offset;
  1035. int pos;
  1036. };
  1037. static void copy_mem_info(struct info_str *info, char *data, int len)
  1038. {
  1039. if (info->pos + len > info->length)
  1040. len = info->length - info->pos;
  1041. if (info->pos + len < info->offset) {
  1042. info->pos += len;
  1043. return;
  1044. }
  1045. if (info->pos < info->offset) {
  1046. data += (info->offset - info->pos);
  1047. len -= (info->offset - info->pos);
  1048. }
  1049. if (len > 0) {
  1050. memcpy(info->buffer + info->pos, data, len);
  1051. info->pos += len;
  1052. }
  1053. }
  1054. static int copy_info(struct info_str *info, char *fmt, ...)
  1055. {
  1056. va_list args;
  1057. char buf[81];
  1058. int len;
  1059. va_start(args, fmt);
  1060. len = vsprintf(buf, fmt, args);
  1061. va_end(args);
  1062. copy_mem_info(info, buf, len);
  1063. return len;
  1064. }
  1065. /*
  1066. * Copy formatted information into the input buffer.
  1067. */
  1068. static int sym_host_info(struct Scsi_Host *shost, char *ptr, off_t offset, int len)
  1069. {
  1070. struct sym_data *sym_data = shost_priv(shost);
  1071. struct pci_dev *pdev = sym_data->pdev;
  1072. struct sym_hcb *np = sym_data->ncb;
  1073. struct info_str info;
  1074. info.buffer = ptr;
  1075. info.length = len;
  1076. info.offset = offset;
  1077. info.pos = 0;
  1078. copy_info(&info, "Chip " NAME53C "%s, device id 0x%x, "
  1079. "revision id 0x%x\n", np->s.chip_name,
  1080. pdev->device, pdev->revision);
  1081. copy_info(&info, "At PCI address %s, IRQ %u\n",
  1082. pci_name(pdev), pdev->irq);
  1083. copy_info(&info, "Min. period factor %d, %s SCSI BUS%s\n",
  1084. (int) (np->minsync_dt ? np->minsync_dt : np->minsync),
  1085. np->maxwide ? "Wide" : "Narrow",
  1086. np->minsync_dt ? ", DT capable" : "");
  1087. copy_info(&info, "Max. started commands %d, "
  1088. "max. commands per LUN %d\n",
  1089. SYM_CONF_MAX_START, SYM_CONF_MAX_TAG);
  1090. return info.pos > info.offset? info.pos - info.offset : 0;
  1091. }
  1092. #endif /* SYM_LINUX_USER_INFO_SUPPORT */
  1093. /*
  1094. * Entry point of the scsi proc fs of the driver.
  1095. * - func = 0 means read (returns adapter infos)
  1096. * - func = 1 means write (not yet merget from sym53c8xx)
  1097. */
  1098. static int sym53c8xx_proc_info(struct Scsi_Host *shost, char *buffer,
  1099. char **start, off_t offset, int length, int func)
  1100. {
  1101. int retv;
  1102. if (func) {
  1103. #ifdef SYM_LINUX_USER_COMMAND_SUPPORT
  1104. retv = sym_user_command(shost, buffer, length);
  1105. #else
  1106. retv = -EINVAL;
  1107. #endif
  1108. } else {
  1109. if (start)
  1110. *start = buffer;
  1111. #ifdef SYM_LINUX_USER_INFO_SUPPORT
  1112. retv = sym_host_info(shost, buffer, offset, length);
  1113. #else
  1114. retv = -EINVAL;
  1115. #endif
  1116. }
  1117. return retv;
  1118. }
  1119. #endif /* SYM_LINUX_PROC_INFO_SUPPORT */
  1120. /*
  1121. * Free resources claimed by sym_iomap_device(). Note that
  1122. * sym_free_resources() should be used instead of this function after calling
  1123. * sym_attach().
  1124. */
  1125. static void __devinit
  1126. sym_iounmap_device(struct sym_device *device)
  1127. {
  1128. if (device->s.ioaddr)
  1129. pci_iounmap(device->pdev, device->s.ioaddr);
  1130. if (device->s.ramaddr)
  1131. pci_iounmap(device->pdev, device->s.ramaddr);
  1132. }
  1133. /*
  1134. * Free controller resources.
  1135. */
  1136. static void sym_free_resources(struct sym_hcb *np, struct pci_dev *pdev,
  1137. int do_free_irq)
  1138. {
  1139. /*
  1140. * Free O/S specific resources.
  1141. */
  1142. if (do_free_irq)
  1143. free_irq(pdev->irq, np->s.host);
  1144. if (np->s.ioaddr)
  1145. pci_iounmap(pdev, np->s.ioaddr);
  1146. if (np->s.ramaddr)
  1147. pci_iounmap(pdev, np->s.ramaddr);
  1148. /*
  1149. * Free O/S independent resources.
  1150. */
  1151. sym_hcb_free(np);
  1152. sym_mfree_dma(np, sizeof(*np), "HCB");
  1153. }
  1154. /*
  1155. * Host attach and initialisations.
  1156. *
  1157. * Allocate host data and ncb structure.
  1158. * Remap MMIO region.
  1159. * Do chip initialization.
  1160. * If all is OK, install interrupt handling and
  1161. * start the timer daemon.
  1162. */
  1163. static struct Scsi_Host * __devinit sym_attach(struct scsi_host_template *tpnt,
  1164. int unit, struct sym_device *dev)
  1165. {
  1166. struct sym_data *sym_data;
  1167. struct sym_hcb *np = NULL;
  1168. struct Scsi_Host *shost = NULL;
  1169. struct pci_dev *pdev = dev->pdev;
  1170. unsigned long flags;
  1171. struct sym_fw *fw;
  1172. int do_free_irq = 0;
  1173. printk(KERN_INFO "sym%d: <%s> rev 0x%x at pci %s irq %u\n",
  1174. unit, dev->chip.name, pdev->revision, pci_name(pdev),
  1175. pdev->irq);
  1176. /*
  1177. * Get the firmware for this chip.
  1178. */
  1179. fw = sym_find_firmware(&dev->chip);
  1180. if (!fw)
  1181. goto attach_failed;
  1182. shost = scsi_host_alloc(tpnt, sizeof(*sym_data));
  1183. if (!shost)
  1184. goto attach_failed;
  1185. sym_data = shost_priv(shost);
  1186. /*
  1187. * Allocate immediately the host control block,
  1188. * since we are only expecting to succeed. :)
  1189. * We keep track in the HCB of all the resources that
  1190. * are to be released on error.
  1191. */
  1192. np = __sym_calloc_dma(&pdev->dev, sizeof(*np), "HCB");
  1193. if (!np)
  1194. goto attach_failed;
  1195. np->bus_dmat = &pdev->dev; /* Result in 1 DMA pool per HBA */
  1196. sym_data->ncb = np;
  1197. sym_data->pdev = pdev;
  1198. np->s.host = shost;
  1199. pci_set_drvdata(pdev, shost);
  1200. /*
  1201. * Copy some useful infos to the HCB.
  1202. */
  1203. np->hcb_ba = vtobus(np);
  1204. np->verbose = sym_driver_setup.verbose;
  1205. np->s.unit = unit;
  1206. np->features = dev->chip.features;
  1207. np->clock_divn = dev->chip.nr_divisor;
  1208. np->maxoffs = dev->chip.offset_max;
  1209. np->maxburst = dev->chip.burst_max;
  1210. np->myaddr = dev->host_id;
  1211. np->mmio_ba = (u32)dev->mmio_base;
  1212. np->ram_ba = (u32)dev->ram_base;
  1213. np->s.ioaddr = dev->s.ioaddr;
  1214. np->s.ramaddr = dev->s.ramaddr;
  1215. /*
  1216. * Edit its name.
  1217. */
  1218. strlcpy(np->s.chip_name, dev->chip.name, sizeof(np->s.chip_name));
  1219. sprintf(np->s.inst_name, "sym%d", np->s.unit);
  1220. if ((SYM_CONF_DMA_ADDRESSING_MODE > 0) && (np->features & FE_DAC) &&
  1221. !pci_set_dma_mask(pdev, DMA_DAC_MASK)) {
  1222. set_dac(np);
  1223. } else if (pci_set_dma_mask(pdev, DMA_BIT_MASK(32))) {
  1224. printf_warning("%s: No suitable DMA available\n", sym_name(np));
  1225. goto attach_failed;
  1226. }
  1227. if (sym_hcb_attach(shost, fw, dev->nvram))
  1228. goto attach_failed;
  1229. /*
  1230. * Install the interrupt handler.
  1231. * If we synchonize the C code with SCRIPTS on interrupt,
  1232. * we do not want to share the INTR line at all.
  1233. */
  1234. if (request_irq(pdev->irq, sym53c8xx_intr, IRQF_SHARED, NAME53C8XX,
  1235. shost)) {
  1236. printf_err("%s: request irq %u failure\n",
  1237. sym_name(np), pdev->irq);
  1238. goto attach_failed;
  1239. }
  1240. do_free_irq = 1;
  1241. /*
  1242. * After SCSI devices have been opened, we cannot
  1243. * reset the bus safely, so we do it here.
  1244. */
  1245. spin_lock_irqsave(shost->host_lock, flags);
  1246. if (sym_reset_scsi_bus(np, 0))
  1247. goto reset_failed;
  1248. /*
  1249. * Start the SCRIPTS.
  1250. */
  1251. sym_start_up(shost, 1);
  1252. /*
  1253. * Start the timer daemon
  1254. */
  1255. init_timer(&np->s.timer);
  1256. np->s.timer.data = (unsigned long) np;
  1257. np->s.timer.function = sym53c8xx_timer;
  1258. np->s.lasttime=0;
  1259. sym_timer (np);
  1260. /*
  1261. * Fill Linux host instance structure
  1262. * and return success.
  1263. */
  1264. shost->max_channel = 0;
  1265. shost->this_id = np->myaddr;
  1266. shost->max_id = np->maxwide ? 16 : 8;
  1267. shost->max_lun = SYM_CONF_MAX_LUN;
  1268. shost->unique_id = pci_resource_start(pdev, 0);
  1269. shost->cmd_per_lun = SYM_CONF_MAX_TAG;
  1270. shost->can_queue = (SYM_CONF_MAX_START-2);
  1271. shost->sg_tablesize = SYM_CONF_MAX_SG;
  1272. shost->max_cmd_len = 16;
  1273. BUG_ON(sym2_transport_template == NULL);
  1274. shost->transportt = sym2_transport_template;
  1275. /* 53c896 rev 1 errata: DMA may not cross 16MB boundary */
  1276. if (pdev->device == PCI_DEVICE_ID_NCR_53C896 && pdev->revision < 2)
  1277. shost->dma_boundary = 0xFFFFFF;
  1278. spin_unlock_irqrestore(shost->host_lock, flags);
  1279. return shost;
  1280. reset_failed:
  1281. printf_err("%s: FATAL ERROR: CHECK SCSI BUS - CABLES, "
  1282. "TERMINATION, DEVICE POWER etc.!\n", sym_name(np));
  1283. spin_unlock_irqrestore(shost->host_lock, flags);
  1284. attach_failed:
  1285. printf_info("sym%d: giving up ...\n", unit);
  1286. if (np)
  1287. sym_free_resources(np, pdev, do_free_irq);
  1288. else
  1289. sym_iounmap_device(dev);
  1290. if (shost)
  1291. scsi_host_put(shost);
  1292. return NULL;
  1293. }
  1294. /*
  1295. * Detect and try to read SYMBIOS and TEKRAM NVRAM.
  1296. */
  1297. #if SYM_CONF_NVRAM_SUPPORT
  1298. static void __devinit sym_get_nvram(struct sym_device *devp, struct sym_nvram *nvp)
  1299. {
  1300. devp->nvram = nvp;
  1301. nvp->type = 0;
  1302. sym_read_nvram(devp, nvp);
  1303. }
  1304. #else
  1305. static inline void sym_get_nvram(struct sym_device *devp, struct sym_nvram *nvp)
  1306. {
  1307. }
  1308. #endif /* SYM_CONF_NVRAM_SUPPORT */
  1309. static int __devinit sym_check_supported(struct sym_device *device)
  1310. {
  1311. struct sym_chip *chip;
  1312. struct pci_dev *pdev = device->pdev;
  1313. unsigned long io_port = pci_resource_start(pdev, 0);
  1314. int i;
  1315. /*
  1316. * If user excluded this chip, do not initialize it.
  1317. * I hate this code so much. Must kill it.
  1318. */
  1319. if (io_port) {
  1320. for (i = 0 ; i < 8 ; i++) {
  1321. if (sym_driver_setup.excludes[i] == io_port)
  1322. return -ENODEV;
  1323. }
  1324. }
  1325. /*
  1326. * Check if the chip is supported. Then copy the chip description
  1327. * to our device structure so we can make it match the actual device
  1328. * and options.
  1329. */
  1330. chip = sym_lookup_chip_table(pdev->device, pdev->revision);
  1331. if (!chip) {
  1332. dev_info(&pdev->dev, "device not supported\n");
  1333. return -ENODEV;
  1334. }
  1335. memcpy(&device->chip, chip, sizeof(device->chip));
  1336. return 0;
  1337. }
  1338. /*
  1339. * Ignore Symbios chips controlled by various RAID controllers.
  1340. * These controllers set value 0x52414944 at RAM end - 16.
  1341. */
  1342. static int __devinit sym_check_raid(struct sym_device *device)
  1343. {
  1344. unsigned int ram_size, ram_val;
  1345. if (!device->s.ramaddr)
  1346. return 0;
  1347. if (device->chip.features & FE_RAM8K)
  1348. ram_size = 8192;
  1349. else
  1350. ram_size = 4096;
  1351. ram_val = readl(device->s.ramaddr + ram_size - 16);
  1352. if (ram_val != 0x52414944)
  1353. return 0;
  1354. dev_info(&device->pdev->dev,
  1355. "not initializing, driven by RAID controller.\n");
  1356. return -ENODEV;
  1357. }
  1358. static int __devinit sym_set_workarounds(struct sym_device *device)
  1359. {
  1360. struct sym_chip *chip = &device->chip;
  1361. struct pci_dev *pdev = device->pdev;
  1362. u_short status_reg;
  1363. /*
  1364. * (ITEM 12 of a DEL about the 896 I haven't yet).
  1365. * We must ensure the chip will use WRITE AND INVALIDATE.
  1366. * The revision number limit is for now arbitrary.
  1367. */
  1368. if (pdev->device == PCI_DEVICE_ID_NCR_53C896 && pdev->revision < 0x4) {
  1369. chip->features |= (FE_WRIE | FE_CLSE);
  1370. }
  1371. /* If the chip can do Memory Write Invalidate, enable it */
  1372. if (chip->features & FE_WRIE) {
  1373. if (pci_set_mwi(pdev))
  1374. return -ENODEV;
  1375. }
  1376. /*
  1377. * Work around for errant bit in 895A. The 66Mhz
  1378. * capable bit is set erroneously. Clear this bit.
  1379. * (Item 1 DEL 533)
  1380. *
  1381. * Make sure Config space and Features agree.
  1382. *
  1383. * Recall: writes are not normal to status register -
  1384. * write a 1 to clear and a 0 to leave unchanged.
  1385. * Can only reset bits.
  1386. */
  1387. pci_read_config_word(pdev, PCI_STATUS, &status_reg);
  1388. if (chip->features & FE_66MHZ) {
  1389. if (!(status_reg & PCI_STATUS_66MHZ))
  1390. chip->features &= ~FE_66MHZ;
  1391. } else {
  1392. if (status_reg & PCI_STATUS_66MHZ) {
  1393. status_reg = PCI_STATUS_66MHZ;
  1394. pci_write_config_word(pdev, PCI_STATUS, status_reg);
  1395. pci_read_config_word(pdev, PCI_STATUS, &status_reg);
  1396. }
  1397. }
  1398. return 0;
  1399. }
  1400. /*
  1401. * Map HBA registers and on-chip SRAM (if present).
  1402. */
  1403. static int __devinit
  1404. sym_iomap_device(struct sym_device *device)
  1405. {
  1406. struct pci_dev *pdev = device->pdev;
  1407. struct pci_bus_region bus_addr;
  1408. int i = 2;
  1409. pcibios_resource_to_bus(pdev, &bus_addr, &pdev->resource[1]);
  1410. device->mmio_base = bus_addr.start;
  1411. if (device->chip.features & FE_RAM) {
  1412. /*
  1413. * If the BAR is 64-bit, resource 2 will be occupied by the
  1414. * upper 32 bits
  1415. */
  1416. if (!pdev->resource[i].flags)
  1417. i++;
  1418. pcibios_resource_to_bus(pdev, &bus_addr, &pdev->resource[i]);
  1419. device->ram_base = bus_addr.start;
  1420. }
  1421. #ifdef CONFIG_SCSI_SYM53C8XX_MMIO
  1422. if (device->mmio_base)
  1423. device->s.ioaddr = pci_iomap(pdev, 1,
  1424. pci_resource_len(pdev, 1));
  1425. #endif
  1426. if (!device->s.ioaddr)
  1427. device->s.ioaddr = pci_iomap(pdev, 0,
  1428. pci_resource_len(pdev, 0));
  1429. if (!device->s.ioaddr) {
  1430. dev_err(&pdev->dev, "could not map registers; giving up.\n");
  1431. return -EIO;
  1432. }
  1433. if (device->ram_base) {
  1434. device->s.ramaddr = pci_iomap(pdev, i,
  1435. pci_resource_len(pdev, i));
  1436. if (!device->s.ramaddr) {
  1437. dev_warn(&pdev->dev,
  1438. "could not map SRAM; continuing anyway.\n");
  1439. device->ram_base = 0;
  1440. }
  1441. }
  1442. return 0;
  1443. }
  1444. /*
  1445. * The NCR PQS and PDS cards are constructed as a DEC bridge
  1446. * behind which sits a proprietary NCR memory controller and
  1447. * either four or two 53c875s as separate devices. We can tell
  1448. * if an 875 is part of a PQS/PDS or not since if it is, it will
  1449. * be on the same bus as the memory controller. In its usual
  1450. * mode of operation, the 875s are slaved to the memory
  1451. * controller for all transfers. To operate with the Linux
  1452. * driver, the memory controller is disabled and the 875s
  1453. * freed to function independently. The only wrinkle is that
  1454. * the preset SCSI ID (which may be zero) must be read in from
  1455. * a special configuration space register of the 875.
  1456. */
  1457. static void sym_config_pqs(struct pci_dev *pdev, struct sym_device *sym_dev)
  1458. {
  1459. int slot;
  1460. u8 tmp;
  1461. for (slot = 0; slot < 256; slot++) {
  1462. struct pci_dev *memc = pci_get_slot(pdev->bus, slot);
  1463. if (!memc || memc->vendor != 0x101a || memc->device == 0x0009) {
  1464. pci_dev_put(memc);
  1465. continue;
  1466. }
  1467. /* bit 1: allow individual 875 configuration */
  1468. pci_read_config_byte(memc, 0x44, &tmp);
  1469. if ((tmp & 0x2) == 0) {
  1470. tmp |= 0x2;
  1471. pci_write_config_byte(memc, 0x44, tmp);
  1472. }
  1473. /* bit 2: drive individual 875 interrupts to the bus */
  1474. pci_read_config_byte(memc, 0x45, &tmp);
  1475. if ((tmp & 0x4) == 0) {
  1476. tmp |= 0x4;
  1477. pci_write_config_byte(memc, 0x45, tmp);
  1478. }
  1479. pci_dev_put(memc);
  1480. break;
  1481. }
  1482. pci_read_config_byte(pdev, 0x84, &tmp);
  1483. sym_dev->host_id = tmp;
  1484. }
  1485. /*
  1486. * Called before unloading the module.
  1487. * Detach the host.
  1488. * We have to free resources and halt the NCR chip.
  1489. */
  1490. static int sym_detach(struct Scsi_Host *shost, struct pci_dev *pdev)
  1491. {
  1492. struct sym_hcb *np = sym_get_hcb(shost);
  1493. printk("%s: detaching ...\n", sym_name(np));
  1494. del_timer_sync(&np->s.timer);
  1495. /*
  1496. * Reset NCR chip.
  1497. * We should use sym_soft_reset(), but we don't want to do
  1498. * so, since we may not be safe if interrupts occur.
  1499. */
  1500. printk("%s: resetting chip\n", sym_name(np));
  1501. OUTB(np, nc_istat, SRST);
  1502. INB(np, nc_mbox1);
  1503. udelay(10);
  1504. OUTB(np, nc_istat, 0);
  1505. sym_free_resources(np, pdev, 1);
  1506. scsi_host_put(shost);
  1507. return 1;
  1508. }
  1509. /*
  1510. * Driver host template.
  1511. */
  1512. static struct scsi_host_template sym2_template = {
  1513. .module = THIS_MODULE,
  1514. .name = "sym53c8xx",
  1515. .info = sym53c8xx_info,
  1516. .queuecommand = sym53c8xx_queue_command,
  1517. .slave_alloc = sym53c8xx_slave_alloc,
  1518. .slave_configure = sym53c8xx_slave_configure,
  1519. .slave_destroy = sym53c8xx_slave_destroy,
  1520. .eh_abort_handler = sym53c8xx_eh_abort_handler,
  1521. .eh_device_reset_handler = sym53c8xx_eh_device_reset_handler,
  1522. .eh_bus_reset_handler = sym53c8xx_eh_bus_reset_handler,
  1523. .eh_host_reset_handler = sym53c8xx_eh_host_reset_handler,
  1524. .this_id = 7,
  1525. .use_clustering = ENABLE_CLUSTERING,
  1526. .max_sectors = 0xFFFF,
  1527. #ifdef SYM_LINUX_PROC_INFO_SUPPORT
  1528. .proc_info = sym53c8xx_proc_info,
  1529. .proc_name = NAME53C8XX,
  1530. #endif
  1531. };
  1532. static int attach_count;
  1533. static int __devinit sym2_probe(struct pci_dev *pdev,
  1534. const struct pci_device_id *ent)
  1535. {
  1536. struct sym_device sym_dev;
  1537. struct sym_nvram nvram;
  1538. struct Scsi_Host *shost;
  1539. int do_iounmap = 0;
  1540. int do_disable_device = 1;
  1541. memset(&sym_dev, 0, sizeof(sym_dev));
  1542. memset(&nvram, 0, sizeof(nvram));
  1543. sym_dev.pdev = pdev;
  1544. sym_dev.host_id = SYM_SETUP_HOST_ID;
  1545. if (pci_enable_device(pdev))
  1546. goto leave;
  1547. pci_set_master(pdev);
  1548. if (pci_request_regions(pdev, NAME53C8XX))
  1549. goto disable;
  1550. if (sym_check_supported(&sym_dev))
  1551. goto free;
  1552. if (sym_iomap_device(&sym_dev))
  1553. goto free;
  1554. do_iounmap = 1;
  1555. if (sym_check_raid(&sym_dev)) {
  1556. do_disable_device = 0; /* Don't disable the device */
  1557. goto free;
  1558. }
  1559. if (sym_set_workarounds(&sym_dev))
  1560. goto free;
  1561. sym_config_pqs(pdev, &sym_dev);
  1562. sym_get_nvram(&sym_dev, &nvram);
  1563. do_iounmap = 0; /* Don't sym_iounmap_device() after sym_attach(). */
  1564. shost = sym_attach(&sym2_template, attach_count, &sym_dev);
  1565. if (!shost)
  1566. goto free;
  1567. if (scsi_add_host(shost, &pdev->dev))
  1568. goto detach;
  1569. scsi_scan_host(shost);
  1570. attach_count++;
  1571. return 0;
  1572. detach:
  1573. sym_detach(pci_get_drvdata(pdev), pdev);
  1574. free:
  1575. if (do_iounmap)
  1576. sym_iounmap_device(&sym_dev);
  1577. pci_release_regions(pdev);
  1578. disable:
  1579. if (do_disable_device)
  1580. pci_disable_device(pdev);
  1581. leave:
  1582. return -ENODEV;
  1583. }
  1584. static void sym2_remove(struct pci_dev *pdev)
  1585. {
  1586. struct Scsi_Host *shost = pci_get_drvdata(pdev);
  1587. scsi_remove_host(shost);
  1588. sym_detach(shost, pdev);
  1589. pci_release_regions(pdev);
  1590. pci_disable_device(pdev);
  1591. attach_count--;
  1592. }
  1593. /**
  1594. * sym2_io_error_detected() - called when PCI error is detected
  1595. * @pdev: pointer to PCI device
  1596. * @state: current state of the PCI slot
  1597. */
  1598. static pci_ers_result_t sym2_io_error_detected(struct pci_dev *pdev,
  1599. enum pci_channel_state state)
  1600. {
  1601. /* If slot is permanently frozen, turn everything off */
  1602. if (state == pci_channel_io_perm_failure) {
  1603. sym2_remove(pdev);
  1604. return PCI_ERS_RESULT_DISCONNECT;
  1605. }
  1606. disable_irq(pdev->irq);
  1607. pci_disable_device(pdev);
  1608. /* Request that MMIO be enabled, so register dump can be taken. */
  1609. return PCI_ERS_RESULT_CAN_RECOVER;
  1610. }
  1611. /**
  1612. * sym2_io_slot_dump - Enable MMIO and dump debug registers
  1613. * @pdev: pointer to PCI device
  1614. */
  1615. static pci_ers_result_t sym2_io_slot_dump(struct pci_dev *pdev)
  1616. {
  1617. struct Scsi_Host *shost = pci_get_drvdata(pdev);
  1618. sym_dump_registers(shost);
  1619. /* Request a slot reset. */
  1620. return PCI_ERS_RESULT_NEED_RESET;
  1621. }
  1622. /**
  1623. * sym2_reset_workarounds - hardware-specific work-arounds
  1624. *
  1625. * This routine is similar to sym_set_workarounds(), except
  1626. * that, at this point, we already know that the device was
  1627. * successfully initialized at least once before, and so most
  1628. * of the steps taken there are un-needed here.
  1629. */
  1630. static void sym2_reset_workarounds(struct pci_dev *pdev)
  1631. {
  1632. u_short status_reg;
  1633. struct sym_chip *chip;
  1634. chip = sym_lookup_chip_table(pdev->device, pdev->revision);
  1635. /* Work around for errant bit in 895A, in a fashion
  1636. * similar to what is done in sym_set_workarounds().
  1637. */
  1638. pci_read_config_word(pdev, PCI_STATUS, &status_reg);
  1639. if (!(chip->features & FE_66MHZ) && (status_reg & PCI_STATUS_66MHZ)) {
  1640. status_reg = PCI_STATUS_66MHZ;
  1641. pci_write_config_word(pdev, PCI_STATUS, status_reg);
  1642. pci_read_config_word(pdev, PCI_STATUS, &status_reg);
  1643. }
  1644. }
  1645. /**
  1646. * sym2_io_slot_reset() - called when the pci bus has been reset.
  1647. * @pdev: pointer to PCI device
  1648. *
  1649. * Restart the card from scratch.
  1650. */
  1651. static pci_ers_result_t sym2_io_slot_reset(struct pci_dev *pdev)
  1652. {
  1653. struct Scsi_Host *shost = pci_get_drvdata(pdev);
  1654. struct sym_hcb *np = sym_get_hcb(shost);
  1655. printk(KERN_INFO "%s: recovering from a PCI slot reset\n",
  1656. sym_name(np));
  1657. if (pci_enable_device(pdev)) {
  1658. printk(KERN_ERR "%s: Unable to enable after PCI reset\n",
  1659. sym_name(np));
  1660. return PCI_ERS_RESULT_DISCONNECT;
  1661. }
  1662. pci_set_master(pdev);
  1663. enable_irq(pdev->irq);
  1664. /* If the chip can do Memory Write Invalidate, enable it */
  1665. if (np->features & FE_WRIE) {
  1666. if (pci_set_mwi(pdev))
  1667. return PCI_ERS_RESULT_DISCONNECT;
  1668. }
  1669. /* Perform work-arounds, analogous to sym_set_workarounds() */
  1670. sym2_reset_workarounds(pdev);
  1671. /* Perform host reset only on one instance of the card */
  1672. if (PCI_FUNC(pdev->devfn) == 0) {
  1673. if (sym_reset_scsi_bus(np, 0)) {
  1674. printk(KERN_ERR "%s: Unable to reset scsi host\n",
  1675. sym_name(np));
  1676. return PCI_ERS_RESULT_DISCONNECT;
  1677. }
  1678. sym_start_up(shost, 1);
  1679. }
  1680. return PCI_ERS_RESULT_RECOVERED;
  1681. }
  1682. /**
  1683. * sym2_io_resume() - resume normal ops after PCI reset
  1684. * @pdev: pointer to PCI device
  1685. *
  1686. * Called when the error recovery driver tells us that its
  1687. * OK to resume normal operation. Use completion to allow
  1688. * halted scsi ops to resume.
  1689. */
  1690. static void sym2_io_resume(struct pci_dev *pdev)
  1691. {
  1692. struct Scsi_Host *shost = pci_get_drvdata(pdev);
  1693. struct sym_data *sym_data = shost_priv(shost);
  1694. spin_lock_irq(shost->host_lock);
  1695. if (sym_data->io_reset)
  1696. complete_all(sym_data->io_reset);
  1697. spin_unlock_irq(shost->host_lock);
  1698. }
  1699. static void sym2_get_signalling(struct Scsi_Host *shost)
  1700. {
  1701. struct sym_hcb *np = sym_get_hcb(shost);
  1702. enum spi_signal_type type;
  1703. switch (np->scsi_mode) {
  1704. case SMODE_SE:
  1705. type = SPI_SIGNAL_SE;
  1706. break;
  1707. case SMODE_LVD:
  1708. type = SPI_SIGNAL_LVD;
  1709. break;
  1710. case SMODE_HVD:
  1711. type = SPI_SIGNAL_HVD;
  1712. break;
  1713. default:
  1714. type = SPI_SIGNAL_UNKNOWN;
  1715. break;
  1716. }
  1717. spi_signalling(shost) = type;
  1718. }
  1719. static void sym2_set_offset(struct scsi_target *starget, int offset)
  1720. {
  1721. struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
  1722. struct sym_hcb *np = sym_get_hcb(shost);
  1723. struct sym_tcb *tp = &np->target[starget->id];
  1724. tp->tgoal.offset = offset;
  1725. tp->tgoal.check_nego = 1;
  1726. }
  1727. static void sym2_set_period(struct scsi_target *starget, int period)
  1728. {
  1729. struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
  1730. struct sym_hcb *np = sym_get_hcb(shost);
  1731. struct sym_tcb *tp = &np->target[starget->id];
  1732. /* have to have DT for these transfers, but DT will also
  1733. * set width, so check that this is allowed */
  1734. if (period <= np->minsync && spi_width(starget))
  1735. tp->tgoal.dt = 1;
  1736. tp->tgoal.period = period;
  1737. tp->tgoal.check_nego = 1;
  1738. }
  1739. static void sym2_set_width(struct scsi_target *starget, int width)
  1740. {
  1741. struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
  1742. struct sym_hcb *np = sym_get_hcb(shost);
  1743. struct sym_tcb *tp = &np->target[starget->id];
  1744. /* It is illegal to have DT set on narrow transfers. If DT is
  1745. * clear, we must also clear IU and QAS. */
  1746. if (width == 0)
  1747. tp->tgoal.iu = tp->tgoal.dt = tp->tgoal.qas = 0;
  1748. tp->tgoal.width = width;
  1749. tp->tgoal.check_nego = 1;
  1750. }
  1751. static void sym2_set_dt(struct scsi_target *starget, int dt)
  1752. {
  1753. struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
  1754. struct sym_hcb *np = sym_get_hcb(shost);
  1755. struct sym_tcb *tp = &np->target[starget->id];
  1756. /* We must clear QAS and IU if DT is clear */
  1757. if (dt)
  1758. tp->tgoal.dt = 1;
  1759. else
  1760. tp->tgoal.iu = tp->tgoal.dt = tp->tgoal.qas = 0;
  1761. tp->tgoal.check_nego = 1;
  1762. }
  1763. #if 0
  1764. static void sym2_set_iu(struct scsi_target *starget, int iu)
  1765. {
  1766. struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
  1767. struct sym_hcb *np = sym_get_hcb(shost);
  1768. struct sym_tcb *tp = &np->target[starget->id];
  1769. if (iu)
  1770. tp->tgoal.iu = tp->tgoal.dt = 1;
  1771. else
  1772. tp->tgoal.iu = 0;
  1773. tp->tgoal.check_nego = 1;
  1774. }
  1775. static void sym2_set_qas(struct scsi_target *starget, int qas)
  1776. {
  1777. struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
  1778. struct sym_hcb *np = sym_get_hcb(shost);
  1779. struct sym_tcb *tp = &np->target[starget->id];
  1780. if (qas)
  1781. tp->tgoal.dt = tp->tgoal.qas = 1;
  1782. else
  1783. tp->tgoal.qas = 0;
  1784. tp->tgoal.check_nego = 1;
  1785. }
  1786. #endif
  1787. static struct spi_function_template sym2_transport_functions = {
  1788. .set_offset = sym2_set_offset,
  1789. .show_offset = 1,
  1790. .set_period = sym2_set_period,
  1791. .show_period = 1,
  1792. .set_width = sym2_set_width,
  1793. .show_width = 1,
  1794. .set_dt = sym2_set_dt,
  1795. .show_dt = 1,
  1796. #if 0
  1797. .set_iu = sym2_set_iu,
  1798. .show_iu = 1,
  1799. .set_qas = sym2_set_qas,
  1800. .show_qas = 1,
  1801. #endif
  1802. .get_signalling = sym2_get_signalling,
  1803. };
  1804. static struct pci_device_id sym2_id_table[] __devinitdata = {
  1805. { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_NCR_53C810,
  1806. PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0UL },
  1807. { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_NCR_53C820,
  1808. PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0UL }, /* new */
  1809. { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_NCR_53C825,
  1810. PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0UL },
  1811. { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_NCR_53C815,
  1812. PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0UL },
  1813. { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_53C810AP,
  1814. PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0UL }, /* new */
  1815. { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_NCR_53C860,
  1816. PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0UL },
  1817. { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_53C1510,
  1818. PCI_ANY_ID, PCI_ANY_ID, PCI_CLASS_STORAGE_SCSI<<8, 0xffff00, 0UL },
  1819. { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_NCR_53C896,
  1820. PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0UL },
  1821. { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_NCR_53C895,
  1822. PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0UL },
  1823. { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_NCR_53C885,
  1824. PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0UL },
  1825. { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_NCR_53C875,
  1826. PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0UL },
  1827. { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_NCR_53C1510,
  1828. PCI_ANY_ID, PCI_ANY_ID, PCI_CLASS_STORAGE_SCSI<<8, 0xffff00, 0UL }, /* new */
  1829. { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_53C895A,
  1830. PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0UL },
  1831. { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_53C875A,
  1832. PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0UL },
  1833. { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_53C1010_33,
  1834. PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0UL },
  1835. { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_53C1010_66,
  1836. PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0UL },
  1837. { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_NCR_53C875J,
  1838. PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0UL },
  1839. { 0, }
  1840. };
  1841. MODULE_DEVICE_TABLE(pci, sym2_id_table);
  1842. static struct pci_error_handlers sym2_err_handler = {
  1843. .error_detected = sym2_io_error_detected,
  1844. .mmio_enabled = sym2_io_slot_dump,
  1845. .slot_reset = sym2_io_slot_reset,
  1846. .resume = sym2_io_resume,
  1847. };
  1848. static struct pci_driver sym2_driver = {
  1849. .name = NAME53C8XX,
  1850. .id_table = sym2_id_table,
  1851. .probe = sym2_probe,
  1852. .remove = sym2_remove,
  1853. .err_handler = &sym2_err_handler,
  1854. };
  1855. static int __init sym2_init(void)
  1856. {
  1857. int error;
  1858. sym2_setup_params();
  1859. sym2_transport_template = spi_attach_transport(&sym2_transport_functions);
  1860. if (!sym2_transport_template)
  1861. return -ENODEV;
  1862. error = pci_register_driver(&sym2_driver);
  1863. if (error)
  1864. spi_release_transport(sym2_transport_template);
  1865. return error;
  1866. }
  1867. static void __exit sym2_exit(void)
  1868. {
  1869. pci_unregister_driver(&sym2_driver);
  1870. spi_release_transport(sym2_transport_template);
  1871. }
  1872. module_init(sym2_init);
  1873. module_exit(sym2_exit);