sym_glue.c 53 KB

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