sym_glue.c 53 KB

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