sym_glue.c 53 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098
  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/interrupt.h>
  43. #include <linux/module.h>
  44. #include <linux/moduleparam.h>
  45. #include <linux/spinlock.h>
  46. #include <scsi/scsi.h>
  47. #include <scsi/scsi_tcq.h>
  48. #include <scsi/scsi_device.h>
  49. #include <scsi/scsi_transport.h>
  50. #include "sym_glue.h"
  51. #include "sym_nvram.h"
  52. #define NAME53C "sym53c"
  53. #define NAME53C8XX "sym53c8xx"
  54. #define IRQ_FMT "%d"
  55. #define IRQ_PRM(x) (x)
  56. struct sym_driver_setup sym_driver_setup = SYM_LINUX_DRIVER_SETUP;
  57. unsigned int sym_debug_flags = 0;
  58. static char *excl_string;
  59. static char *safe_string;
  60. module_param_named(cmd_per_lun, sym_driver_setup.max_tag, ushort, 0);
  61. module_param_named(burst, sym_driver_setup.burst_order, byte, 0);
  62. module_param_named(led, sym_driver_setup.scsi_led, byte, 0);
  63. module_param_named(diff, sym_driver_setup.scsi_diff, byte, 0);
  64. module_param_named(irqm, sym_driver_setup.irq_mode, byte, 0);
  65. module_param_named(buschk, sym_driver_setup.scsi_bus_check, byte, 0);
  66. module_param_named(hostid, sym_driver_setup.host_id, byte, 0);
  67. module_param_named(verb, sym_driver_setup.verbose, byte, 0);
  68. module_param_named(debug, sym_debug_flags, uint, 0);
  69. module_param_named(settle, sym_driver_setup.settle_delay, byte, 0);
  70. module_param_named(nvram, sym_driver_setup.use_nvram, byte, 0);
  71. module_param_named(excl, excl_string, charp, 0);
  72. module_param_named(safe, safe_string, charp, 0);
  73. MODULE_PARM_DESC(cmd_per_lun, "The maximum number of tags to use by default");
  74. MODULE_PARM_DESC(burst, "Maximum burst. 0 to disable, 255 to read from registers");
  75. MODULE_PARM_DESC(led, "Set to 1 to enable LED support");
  76. MODULE_PARM_DESC(diff, "0 for no differential mode, 1 for BIOS, 2 for always, 3 for not GPIO3");
  77. MODULE_PARM_DESC(irqm, "0 for open drain, 1 to leave alone, 2 for totem pole");
  78. MODULE_PARM_DESC(buschk, "0 to not check, 1 for detach on error, 2 for warn on error");
  79. MODULE_PARM_DESC(hostid, "The SCSI ID to use for the host adapters");
  80. MODULE_PARM_DESC(verb, "0 for minimal verbosity, 1 for normal, 2 for excessive");
  81. MODULE_PARM_DESC(debug, "Set bits to enable debugging");
  82. MODULE_PARM_DESC(settle, "Settle delay in seconds. Default 3");
  83. MODULE_PARM_DESC(nvram, "Option currently not used");
  84. MODULE_PARM_DESC(excl, "List ioport addresses here to prevent controllers from being attached");
  85. MODULE_PARM_DESC(safe, "Set other settings to a \"safe mode\"");
  86. MODULE_LICENSE("GPL");
  87. MODULE_VERSION(SYM_VERSION);
  88. MODULE_AUTHOR("Matthew Wilcox <matthew@wil.cx>");
  89. MODULE_DESCRIPTION("NCR, Symbios and LSI 8xx and 1010 PCI SCSI adapters");
  90. static void sym2_setup_params(void)
  91. {
  92. char *p = excl_string;
  93. int xi = 0;
  94. while (p && (xi < 8)) {
  95. char *next_p;
  96. int val = (int) simple_strtoul(p, &next_p, 0);
  97. sym_driver_setup.excludes[xi++] = val;
  98. p = next_p;
  99. }
  100. if (safe_string) {
  101. if (*safe_string == 'y') {
  102. sym_driver_setup.max_tag = 0;
  103. sym_driver_setup.burst_order = 0;
  104. sym_driver_setup.scsi_led = 0;
  105. sym_driver_setup.scsi_diff = 1;
  106. sym_driver_setup.irq_mode = 0;
  107. sym_driver_setup.scsi_bus_check = 2;
  108. sym_driver_setup.host_id = 7;
  109. sym_driver_setup.verbose = 2;
  110. sym_driver_setup.settle_delay = 10;
  111. sym_driver_setup.use_nvram = 1;
  112. } else if (*safe_string != 'n') {
  113. printk(KERN_WARNING NAME53C8XX "Ignoring parameter %s"
  114. " passed to safe option", safe_string);
  115. }
  116. }
  117. }
  118. static struct scsi_transport_template *sym2_transport_template = NULL;
  119. /*
  120. * Driver private area in the SCSI command structure.
  121. */
  122. struct sym_ucmd { /* Override the SCSI pointer structure */
  123. struct completion *eh_done; /* SCSI error handling */
  124. };
  125. #define SYM_UCMD_PTR(cmd) ((struct sym_ucmd *)(&(cmd)->SCp))
  126. #define SYM_SOFTC_PTR(cmd) sym_get_hcb(cmd->device->host)
  127. /*
  128. * Complete a pending CAM CCB.
  129. */
  130. void sym_xpt_done(struct sym_hcb *np, struct scsi_cmnd *cmd)
  131. {
  132. struct sym_ucmd *ucmd = SYM_UCMD_PTR(cmd);
  133. BUILD_BUG_ON(sizeof(struct scsi_pointer) < sizeof(struct sym_ucmd));
  134. if (ucmd->eh_done)
  135. complete(ucmd->eh_done);
  136. scsi_dma_unmap(cmd);
  137. cmd->scsi_done(cmd);
  138. }
  139. /*
  140. * Tell the SCSI layer about a BUS RESET.
  141. */
  142. void sym_xpt_async_bus_reset(struct sym_hcb *np)
  143. {
  144. printf_notice("%s: SCSI BUS has been reset.\n", sym_name(np));
  145. np->s.settle_time = jiffies + sym_driver_setup.settle_delay * HZ;
  146. np->s.settle_time_valid = 1;
  147. if (sym_verbose >= 2)
  148. printf_info("%s: command processing suspended for %d seconds\n",
  149. sym_name(np), sym_driver_setup.settle_delay);
  150. }
  151. /*
  152. * Tell the SCSI layer about a BUS DEVICE RESET message sent.
  153. */
  154. void sym_xpt_async_sent_bdr(struct sym_hcb *np, int target)
  155. {
  156. printf_notice("%s: TARGET %d has been reset.\n", sym_name(np), target);
  157. }
  158. /*
  159. * Choose the more appropriate CAM status if
  160. * the IO encountered an extended error.
  161. */
  162. static int sym_xerr_cam_status(int cam_status, int x_status)
  163. {
  164. if (x_status) {
  165. if (x_status & XE_PARITY_ERR)
  166. cam_status = DID_PARITY;
  167. else if (x_status &(XE_EXTRA_DATA|XE_SODL_UNRUN|XE_SWIDE_OVRUN))
  168. cam_status = DID_ERROR;
  169. else if (x_status & XE_BAD_PHASE)
  170. cam_status = DID_ERROR;
  171. else
  172. cam_status = DID_ERROR;
  173. }
  174. return cam_status;
  175. }
  176. /*
  177. * Build CAM result for a failed or auto-sensed IO.
  178. */
  179. void sym_set_cam_result_error(struct sym_hcb *np, struct sym_ccb *cp, int resid)
  180. {
  181. struct scsi_cmnd *cmd = cp->cmd;
  182. u_int cam_status, scsi_status, drv_status;
  183. drv_status = 0;
  184. cam_status = DID_OK;
  185. scsi_status = cp->ssss_status;
  186. if (cp->host_flags & HF_SENSE) {
  187. scsi_status = cp->sv_scsi_status;
  188. resid = cp->sv_resid;
  189. if (sym_verbose && cp->sv_xerr_status)
  190. sym_print_xerr(cmd, cp->sv_xerr_status);
  191. if (cp->host_status == HS_COMPLETE &&
  192. cp->ssss_status == S_GOOD &&
  193. cp->xerr_status == 0) {
  194. cam_status = sym_xerr_cam_status(DID_OK,
  195. cp->sv_xerr_status);
  196. drv_status = DRIVER_SENSE;
  197. /*
  198. * Bounce back the sense data to user.
  199. */
  200. memset(&cmd->sense_buffer, 0, sizeof(cmd->sense_buffer));
  201. memcpy(cmd->sense_buffer, cp->sns_bbuf,
  202. min(sizeof(cmd->sense_buffer),
  203. (size_t)SYM_SNS_BBUF_LEN));
  204. #if 0
  205. /*
  206. * If the device reports a UNIT ATTENTION condition
  207. * due to a RESET condition, we should consider all
  208. * disconnect CCBs for this unit as aborted.
  209. */
  210. if (1) {
  211. u_char *p;
  212. p = (u_char *) cmd->sense_data;
  213. if (p[0]==0x70 && p[2]==0x6 && p[12]==0x29)
  214. sym_clear_tasks(np, DID_ABORT,
  215. cp->target,cp->lun, -1);
  216. }
  217. #endif
  218. } else {
  219. /*
  220. * Error return from our internal request sense. This
  221. * is bad: we must clear the contingent allegiance
  222. * condition otherwise the device will always return
  223. * BUSY. Use a big stick.
  224. */
  225. sym_reset_scsi_target(np, cmd->device->id);
  226. cam_status = DID_ERROR;
  227. }
  228. } else if (cp->host_status == HS_COMPLETE) /* Bad SCSI status */
  229. cam_status = DID_OK;
  230. else if (cp->host_status == HS_SEL_TIMEOUT) /* Selection timeout */
  231. cam_status = DID_NO_CONNECT;
  232. else if (cp->host_status == HS_UNEXPECTED) /* Unexpected BUS FREE*/
  233. cam_status = DID_ERROR;
  234. else { /* Extended error */
  235. if (sym_verbose) {
  236. sym_print_addr(cmd, "COMMAND FAILED (%x %x %x).\n",
  237. cp->host_status, cp->ssss_status,
  238. cp->xerr_status);
  239. }
  240. /*
  241. * Set the most appropriate value for CAM status.
  242. */
  243. cam_status = sym_xerr_cam_status(DID_ERROR, cp->xerr_status);
  244. }
  245. scsi_set_resid(cmd, resid);
  246. cmd->result = (drv_status << 24) + (cam_status << 16) + scsi_status;
  247. }
  248. static int sym_scatter(struct sym_hcb *np, struct sym_ccb *cp, struct scsi_cmnd *cmd)
  249. {
  250. int segment;
  251. int use_sg;
  252. cp->data_len = 0;
  253. use_sg = scsi_dma_map(cmd);
  254. if (use_sg > 0) {
  255. struct scatterlist *sg;
  256. struct sym_tcb *tp = &np->target[cp->target];
  257. struct sym_tblmove *data;
  258. if (use_sg > SYM_CONF_MAX_SG) {
  259. scsi_dma_unmap(cmd);
  260. return -1;
  261. }
  262. data = &cp->phys.data[SYM_CONF_MAX_SG - use_sg];
  263. scsi_for_each_sg(cmd, sg, use_sg, segment) {
  264. dma_addr_t baddr = sg_dma_address(sg);
  265. unsigned int len = sg_dma_len(sg);
  266. if ((len & 1) && (tp->head.wval & EWS)) {
  267. len++;
  268. cp->odd_byte_adjustment++;
  269. }
  270. sym_build_sge(np, &data[segment], baddr, len);
  271. cp->data_len += len;
  272. }
  273. } else {
  274. segment = -2;
  275. }
  276. return segment;
  277. }
  278. /*
  279. * Queue a SCSI command.
  280. */
  281. static int sym_queue_command(struct sym_hcb *np, struct scsi_cmnd *cmd)
  282. {
  283. struct scsi_device *sdev = cmd->device;
  284. struct sym_tcb *tp;
  285. struct sym_lcb *lp;
  286. struct sym_ccb *cp;
  287. int order;
  288. /*
  289. * Retrieve the target descriptor.
  290. */
  291. tp = &np->target[sdev->id];
  292. /*
  293. * Select tagged/untagged.
  294. */
  295. lp = sym_lp(tp, sdev->lun);
  296. order = (lp && lp->s.reqtags) ? M_SIMPLE_TAG : 0;
  297. /*
  298. * Queue the SCSI IO.
  299. */
  300. cp = sym_get_ccb(np, cmd, order);
  301. if (!cp)
  302. return 1; /* Means resource shortage */
  303. sym_queue_scsiio(np, cmd, cp);
  304. return 0;
  305. }
  306. /*
  307. * Setup buffers and pointers that address the CDB.
  308. */
  309. static inline int sym_setup_cdb(struct sym_hcb *np, struct scsi_cmnd *cmd, struct sym_ccb *cp)
  310. {
  311. memcpy(cp->cdb_buf, cmd->cmnd, cmd->cmd_len);
  312. cp->phys.cmd.addr = CCB_BA(cp, cdb_buf[0]);
  313. cp->phys.cmd.size = cpu_to_scr(cmd->cmd_len);
  314. return 0;
  315. }
  316. /*
  317. * Setup pointers that address the data and start the I/O.
  318. */
  319. int sym_setup_data_and_start(struct sym_hcb *np, struct scsi_cmnd *cmd, struct sym_ccb *cp)
  320. {
  321. u32 lastp, goalp;
  322. int dir;
  323. /*
  324. * Build the CDB.
  325. */
  326. if (sym_setup_cdb(np, cmd, cp))
  327. goto out_abort;
  328. /*
  329. * No direction means no data.
  330. */
  331. dir = cmd->sc_data_direction;
  332. if (dir != DMA_NONE) {
  333. cp->segments = sym_scatter(np, cp, cmd);
  334. if (cp->segments < 0) {
  335. sym_set_cam_status(cmd, DID_ERROR);
  336. goto out_abort;
  337. }
  338. /*
  339. * No segments means no data.
  340. */
  341. if (!cp->segments)
  342. dir = DMA_NONE;
  343. } else {
  344. cp->data_len = 0;
  345. cp->segments = 0;
  346. }
  347. /*
  348. * Set the data pointer.
  349. */
  350. switch (dir) {
  351. case DMA_BIDIRECTIONAL:
  352. printk("%s: got DMA_BIDIRECTIONAL command", sym_name(np));
  353. sym_set_cam_status(cmd, DID_ERROR);
  354. goto out_abort;
  355. case DMA_TO_DEVICE:
  356. goalp = SCRIPTA_BA(np, data_out2) + 8;
  357. lastp = goalp - 8 - (cp->segments * (2*4));
  358. break;
  359. case DMA_FROM_DEVICE:
  360. cp->host_flags |= HF_DATA_IN;
  361. goalp = SCRIPTA_BA(np, data_in2) + 8;
  362. lastp = goalp - 8 - (cp->segments * (2*4));
  363. break;
  364. case DMA_NONE:
  365. default:
  366. lastp = goalp = SCRIPTB_BA(np, no_data);
  367. break;
  368. }
  369. /*
  370. * Set all pointers values needed by SCRIPTS.
  371. */
  372. cp->phys.head.lastp = cpu_to_scr(lastp);
  373. cp->phys.head.savep = cpu_to_scr(lastp);
  374. cp->startp = cp->phys.head.savep;
  375. cp->goalp = cpu_to_scr(goalp);
  376. /*
  377. * When `#ifed 1', the code below makes the driver
  378. * panic on the first attempt to write to a SCSI device.
  379. * It is the first test we want to do after a driver
  380. * change that does not seem obviously safe. :)
  381. */
  382. #if 0
  383. switch (cp->cdb_buf[0]) {
  384. case 0x0A: case 0x2A: case 0xAA:
  385. panic("XXXXXXXXXXXXX WRITE NOT YET ALLOWED XXXXXXXXXXXXXX\n");
  386. break;
  387. default:
  388. break;
  389. }
  390. #endif
  391. /*
  392. * activate this job.
  393. */
  394. sym_put_start_queue(np, cp);
  395. return 0;
  396. out_abort:
  397. sym_free_ccb(np, cp);
  398. sym_xpt_done(np, cmd);
  399. return 0;
  400. }
  401. /*
  402. * timer daemon.
  403. *
  404. * Misused to keep the driver running when
  405. * interrupts are not configured correctly.
  406. */
  407. static void sym_timer(struct sym_hcb *np)
  408. {
  409. unsigned long thistime = jiffies;
  410. /*
  411. * Restart the timer.
  412. */
  413. np->s.timer.expires = thistime + SYM_CONF_TIMER_INTERVAL;
  414. add_timer(&np->s.timer);
  415. /*
  416. * If we are resetting the ncr, wait for settle_time before
  417. * clearing it. Then command processing will be resumed.
  418. */
  419. if (np->s.settle_time_valid) {
  420. if (time_before_eq(np->s.settle_time, thistime)) {
  421. if (sym_verbose >= 2 )
  422. printk("%s: command processing resumed\n",
  423. sym_name(np));
  424. np->s.settle_time_valid = 0;
  425. }
  426. return;
  427. }
  428. /*
  429. * Nothing to do for now, but that may come.
  430. */
  431. if (np->s.lasttime + 4*HZ < thistime) {
  432. np->s.lasttime = thistime;
  433. }
  434. #ifdef SYM_CONF_PCIQ_MAY_MISS_COMPLETIONS
  435. /*
  436. * Some way-broken PCI bridges may lead to
  437. * completions being lost when the clearing
  438. * of the INTFLY flag by the CPU occurs
  439. * concurrently with the chip raising this flag.
  440. * If this ever happen, lost completions will
  441. * be reaped here.
  442. */
  443. sym_wakeup_done(np);
  444. #endif
  445. }
  446. /*
  447. * PCI BUS error handler.
  448. */
  449. void sym_log_bus_error(struct sym_hcb *np)
  450. {
  451. u_short pci_sts;
  452. pci_read_config_word(np->s.device, PCI_STATUS, &pci_sts);
  453. if (pci_sts & 0xf900) {
  454. pci_write_config_word(np->s.device, PCI_STATUS, pci_sts);
  455. printf("%s: PCI STATUS = 0x%04x\n",
  456. sym_name(np), pci_sts & 0xf900);
  457. }
  458. }
  459. /*
  460. * queuecommand method. Entered with the host adapter lock held and
  461. * interrupts disabled.
  462. */
  463. static int sym53c8xx_queue_command(struct scsi_cmnd *cmd,
  464. void (*done)(struct scsi_cmnd *))
  465. {
  466. struct sym_hcb *np = SYM_SOFTC_PTR(cmd);
  467. struct sym_ucmd *ucp = SYM_UCMD_PTR(cmd);
  468. int sts = 0;
  469. cmd->scsi_done = done;
  470. memset(ucp, 0, sizeof(*ucp));
  471. /*
  472. * Shorten our settle_time if needed for
  473. * this command not to time out.
  474. */
  475. if (np->s.settle_time_valid && cmd->timeout_per_command) {
  476. unsigned long tlimit = jiffies + cmd->timeout_per_command;
  477. tlimit -= SYM_CONF_TIMER_INTERVAL*2;
  478. if (time_after(np->s.settle_time, tlimit)) {
  479. np->s.settle_time = tlimit;
  480. }
  481. }
  482. if (np->s.settle_time_valid)
  483. return SCSI_MLQUEUE_HOST_BUSY;
  484. sts = sym_queue_command(np, cmd);
  485. if (sts)
  486. return SCSI_MLQUEUE_HOST_BUSY;
  487. return 0;
  488. }
  489. /*
  490. * Linux entry point of the interrupt handler.
  491. */
  492. static irqreturn_t sym53c8xx_intr(int irq, void *dev_id)
  493. {
  494. struct sym_hcb *np = dev_id;
  495. /* Avoid spinloop trying to handle interrupts on frozen device */
  496. if (pci_channel_offline(np->s.device))
  497. return IRQ_NONE;
  498. if (DEBUG_FLAGS & DEBUG_TINY) printf_debug ("[");
  499. spin_lock(np->s.host->host_lock);
  500. sym_interrupt(np);
  501. spin_unlock(np->s.host->host_lock);
  502. if (DEBUG_FLAGS & DEBUG_TINY) printf_debug ("]\n");
  503. return IRQ_HANDLED;
  504. }
  505. /*
  506. * Linux entry point of the timer handler
  507. */
  508. static void sym53c8xx_timer(unsigned long npref)
  509. {
  510. struct sym_hcb *np = (struct sym_hcb *)npref;
  511. unsigned long flags;
  512. spin_lock_irqsave(np->s.host->host_lock, flags);
  513. sym_timer(np);
  514. spin_unlock_irqrestore(np->s.host->host_lock, flags);
  515. }
  516. /*
  517. * What the eh thread wants us to perform.
  518. */
  519. #define SYM_EH_ABORT 0
  520. #define SYM_EH_DEVICE_RESET 1
  521. #define SYM_EH_BUS_RESET 2
  522. #define SYM_EH_HOST_RESET 3
  523. /*
  524. * Generic method for our eh processing.
  525. * The 'op' argument tells what we have to do.
  526. */
  527. static int sym_eh_handler(int op, char *opname, struct scsi_cmnd *cmd)
  528. {
  529. struct sym_hcb *np = SYM_SOFTC_PTR(cmd);
  530. struct sym_ucmd *ucmd = SYM_UCMD_PTR(cmd);
  531. struct Scsi_Host *host = cmd->device->host;
  532. struct pci_dev *pdev = np->s.device;
  533. SYM_QUEHEAD *qp;
  534. int cmd_queued = 0;
  535. int sts = -1;
  536. struct completion eh_done;
  537. dev_warn(&cmd->device->sdev_gendev, "%s operation started.\n", opname);
  538. /* We may be in an error condition because the PCI bus
  539. * went down. In this case, we need to wait until the
  540. * PCI bus is reset, the card is reset, and only then
  541. * proceed with the scsi error recovery. There's no
  542. * point in hurrying; take a leisurely wait.
  543. */
  544. #define WAIT_FOR_PCI_RECOVERY 35
  545. if (pci_channel_offline(pdev)) {
  546. struct host_data *hostdata = shost_priv(host);
  547. struct completion *io_reset;
  548. int finished_reset = 0;
  549. init_completion(&eh_done);
  550. spin_lock_irq(host->host_lock);
  551. /* Make sure we didn't race */
  552. if (pci_channel_offline(pdev)) {
  553. if (!hostdata->io_reset)
  554. hostdata->io_reset = &eh_done;
  555. io_reset = hostdata->io_reset;
  556. } else {
  557. io_reset = NULL;
  558. }
  559. if (!pci_channel_offline(pdev))
  560. finished_reset = 1;
  561. spin_unlock_irq(host->host_lock);
  562. if (!finished_reset)
  563. finished_reset = wait_for_completion_timeout(io_reset,
  564. WAIT_FOR_PCI_RECOVERY*HZ);
  565. if (!finished_reset)
  566. return SCSI_FAILED;
  567. }
  568. spin_lock_irq(host->host_lock);
  569. /* This one is queued in some place -> to wait for completion */
  570. FOR_EACH_QUEUED_ELEMENT(&np->busy_ccbq, qp) {
  571. struct sym_ccb *cp = sym_que_entry(qp, struct sym_ccb, link_ccbq);
  572. if (cp->cmd == cmd) {
  573. cmd_queued = 1;
  574. break;
  575. }
  576. }
  577. /* Try to proceed the operation we have been asked for */
  578. sts = -1;
  579. switch(op) {
  580. case SYM_EH_ABORT:
  581. sts = sym_abort_scsiio(np, cmd, 1);
  582. break;
  583. case SYM_EH_DEVICE_RESET:
  584. sts = sym_reset_scsi_target(np, cmd->device->id);
  585. break;
  586. case SYM_EH_BUS_RESET:
  587. sym_reset_scsi_bus(np, 1);
  588. sts = 0;
  589. break;
  590. case SYM_EH_HOST_RESET:
  591. sym_reset_scsi_bus(np, 0);
  592. sym_start_up(np, 1);
  593. sts = 0;
  594. break;
  595. default:
  596. break;
  597. }
  598. /* On error, restore everything and cross fingers :) */
  599. if (sts)
  600. cmd_queued = 0;
  601. if (cmd_queued) {
  602. init_completion(&eh_done);
  603. ucmd->eh_done = &eh_done;
  604. spin_unlock_irq(host->host_lock);
  605. if (!wait_for_completion_timeout(&eh_done, 5*HZ)) {
  606. ucmd->eh_done = NULL;
  607. sts = -2;
  608. }
  609. } else {
  610. spin_unlock_irq(host->host_lock);
  611. }
  612. dev_warn(&cmd->device->sdev_gendev, "%s operation %s.\n", opname,
  613. sts==0 ? "complete" :sts==-2 ? "timed-out" : "failed");
  614. return sts ? SCSI_FAILED : SCSI_SUCCESS;
  615. }
  616. /*
  617. * Error handlers called from the eh thread (one thread per HBA).
  618. */
  619. static int sym53c8xx_eh_abort_handler(struct scsi_cmnd *cmd)
  620. {
  621. return sym_eh_handler(SYM_EH_ABORT, "ABORT", cmd);
  622. }
  623. static int sym53c8xx_eh_device_reset_handler(struct scsi_cmnd *cmd)
  624. {
  625. return sym_eh_handler(SYM_EH_DEVICE_RESET, "DEVICE RESET", cmd);
  626. }
  627. static int sym53c8xx_eh_bus_reset_handler(struct scsi_cmnd *cmd)
  628. {
  629. return sym_eh_handler(SYM_EH_BUS_RESET, "BUS RESET", cmd);
  630. }
  631. static int sym53c8xx_eh_host_reset_handler(struct scsi_cmnd *cmd)
  632. {
  633. return sym_eh_handler(SYM_EH_HOST_RESET, "HOST RESET", cmd);
  634. }
  635. /*
  636. * Tune device queuing depth, according to various limits.
  637. */
  638. static void sym_tune_dev_queuing(struct sym_tcb *tp, int lun, u_short reqtags)
  639. {
  640. struct sym_lcb *lp = sym_lp(tp, lun);
  641. u_short oldtags;
  642. if (!lp)
  643. return;
  644. oldtags = lp->s.reqtags;
  645. if (reqtags > lp->s.scdev_depth)
  646. reqtags = lp->s.scdev_depth;
  647. lp->s.reqtags = reqtags;
  648. if (reqtags != oldtags) {
  649. dev_info(&tp->starget->dev,
  650. "tagged command queuing %s, command queue depth %d.\n",
  651. lp->s.reqtags ? "enabled" : "disabled", reqtags);
  652. }
  653. }
  654. static int sym53c8xx_slave_alloc(struct scsi_device *sdev)
  655. {
  656. struct sym_hcb *np = sym_get_hcb(sdev->host);
  657. struct sym_tcb *tp = &np->target[sdev->id];
  658. struct sym_lcb *lp;
  659. if (sdev->id >= SYM_CONF_MAX_TARGET || sdev->lun >= SYM_CONF_MAX_LUN)
  660. return -ENXIO;
  661. tp->starget = sdev->sdev_target;
  662. /*
  663. * Fail the device init if the device is flagged NOSCAN at BOOT in
  664. * the NVRAM. This may speed up boot and maintain coherency with
  665. * BIOS device numbering. Clearing the flag allows the user to
  666. * rescan skipped devices later. We also return an error for
  667. * devices not flagged for SCAN LUNS in the NVRAM since some single
  668. * lun devices behave badly when asked for a non zero LUN.
  669. */
  670. if (tp->usrflags & SYM_SCAN_BOOT_DISABLED) {
  671. tp->usrflags &= ~SYM_SCAN_BOOT_DISABLED;
  672. starget_printk(KERN_INFO, tp->starget,
  673. "Scan at boot disabled in NVRAM\n");
  674. return -ENXIO;
  675. }
  676. if (tp->usrflags & SYM_SCAN_LUNS_DISABLED) {
  677. if (sdev->lun != 0)
  678. return -ENXIO;
  679. starget_printk(KERN_INFO, tp->starget,
  680. "Multiple LUNs disabled in NVRAM\n");
  681. }
  682. lp = sym_alloc_lcb(np, sdev->id, sdev->lun);
  683. if (!lp)
  684. return -ENOMEM;
  685. spi_min_period(tp->starget) = tp->usr_period;
  686. spi_max_width(tp->starget) = tp->usr_width;
  687. return 0;
  688. }
  689. /*
  690. * Linux entry point for device queue sizing.
  691. */
  692. static int sym53c8xx_slave_configure(struct scsi_device *sdev)
  693. {
  694. struct sym_hcb *np = sym_get_hcb(sdev->host);
  695. struct sym_tcb *tp = &np->target[sdev->id];
  696. struct sym_lcb *lp = sym_lp(tp, sdev->lun);
  697. int reqtags, depth_to_use;
  698. /*
  699. * Get user flags.
  700. */
  701. lp->curr_flags = lp->user_flags;
  702. /*
  703. * Select queue depth from driver setup.
  704. * Donnot use more than configured by user.
  705. * Use at least 2.
  706. * Donnot use more than our maximum.
  707. */
  708. reqtags = sym_driver_setup.max_tag;
  709. if (reqtags > tp->usrtags)
  710. reqtags = tp->usrtags;
  711. if (!sdev->tagged_supported)
  712. reqtags = 0;
  713. if (reqtags > SYM_CONF_MAX_TAG)
  714. reqtags = SYM_CONF_MAX_TAG;
  715. depth_to_use = reqtags ? reqtags : 2;
  716. scsi_adjust_queue_depth(sdev,
  717. sdev->tagged_supported ? MSG_SIMPLE_TAG : 0,
  718. depth_to_use);
  719. lp->s.scdev_depth = depth_to_use;
  720. sym_tune_dev_queuing(tp, sdev->lun, reqtags);
  721. if (!spi_initial_dv(sdev->sdev_target))
  722. spi_dv_device(sdev);
  723. return 0;
  724. }
  725. static void sym53c8xx_slave_destroy(struct scsi_device *sdev)
  726. {
  727. struct sym_hcb *np = sym_get_hcb(sdev->host);
  728. struct sym_lcb *lp = sym_lp(&np->target[sdev->id], sdev->lun);
  729. if (lp->itlq_tbl)
  730. sym_mfree_dma(lp->itlq_tbl, SYM_CONF_MAX_TASK * 4, "ITLQ_TBL");
  731. kfree(lp->cb_tags);
  732. sym_mfree_dma(lp, sizeof(*lp), "LCB");
  733. }
  734. /*
  735. * Linux entry point for info() function
  736. */
  737. static const char *sym53c8xx_info (struct Scsi_Host *host)
  738. {
  739. return SYM_DRIVER_NAME;
  740. }
  741. #ifdef SYM_LINUX_PROC_INFO_SUPPORT
  742. /*
  743. * Proc file system stuff
  744. *
  745. * A read operation returns adapter information.
  746. * A write operation is a control command.
  747. * The string is parsed in the driver code and the command is passed
  748. * to the sym_usercmd() function.
  749. */
  750. #ifdef SYM_LINUX_USER_COMMAND_SUPPORT
  751. struct sym_usrcmd {
  752. u_long target;
  753. u_long lun;
  754. u_long data;
  755. u_long cmd;
  756. };
  757. #define UC_SETSYNC 10
  758. #define UC_SETTAGS 11
  759. #define UC_SETDEBUG 12
  760. #define UC_SETWIDE 14
  761. #define UC_SETFLAG 15
  762. #define UC_SETVERBOSE 17
  763. #define UC_RESETDEV 18
  764. #define UC_CLEARDEV 19
  765. static void sym_exec_user_command (struct sym_hcb *np, struct sym_usrcmd *uc)
  766. {
  767. struct sym_tcb *tp;
  768. int t, l;
  769. switch (uc->cmd) {
  770. case 0: return;
  771. #ifdef SYM_LINUX_DEBUG_CONTROL_SUPPORT
  772. case UC_SETDEBUG:
  773. sym_debug_flags = uc->data;
  774. break;
  775. #endif
  776. case UC_SETVERBOSE:
  777. np->verbose = uc->data;
  778. break;
  779. default:
  780. /*
  781. * We assume that other commands apply to targets.
  782. * This should always be the case and avoid the below
  783. * 4 lines to be repeated 6 times.
  784. */
  785. for (t = 0; t < SYM_CONF_MAX_TARGET; t++) {
  786. if (!((uc->target >> t) & 1))
  787. continue;
  788. tp = &np->target[t];
  789. switch (uc->cmd) {
  790. case UC_SETSYNC:
  791. if (!uc->data || uc->data >= 255) {
  792. tp->tgoal.iu = tp->tgoal.dt =
  793. tp->tgoal.qas = 0;
  794. tp->tgoal.offset = 0;
  795. } else if (uc->data <= 9 && np->minsync_dt) {
  796. if (uc->data < np->minsync_dt)
  797. uc->data = np->minsync_dt;
  798. tp->tgoal.iu = tp->tgoal.dt =
  799. tp->tgoal.qas = 1;
  800. tp->tgoal.width = 1;
  801. tp->tgoal.period = uc->data;
  802. tp->tgoal.offset = np->maxoffs_dt;
  803. } else {
  804. if (uc->data < np->minsync)
  805. uc->data = np->minsync;
  806. tp->tgoal.iu = tp->tgoal.dt =
  807. tp->tgoal.qas = 0;
  808. tp->tgoal.period = uc->data;
  809. tp->tgoal.offset = np->maxoffs;
  810. }
  811. tp->tgoal.check_nego = 1;
  812. break;
  813. case UC_SETWIDE:
  814. tp->tgoal.width = uc->data ? 1 : 0;
  815. tp->tgoal.check_nego = 1;
  816. break;
  817. case UC_SETTAGS:
  818. for (l = 0; l < SYM_CONF_MAX_LUN; l++)
  819. sym_tune_dev_queuing(tp, l, uc->data);
  820. break;
  821. case UC_RESETDEV:
  822. tp->to_reset = 1;
  823. np->istat_sem = SEM;
  824. OUTB(np, nc_istat, SIGP|SEM);
  825. break;
  826. case UC_CLEARDEV:
  827. for (l = 0; l < SYM_CONF_MAX_LUN; l++) {
  828. struct sym_lcb *lp = sym_lp(tp, l);
  829. if (lp) lp->to_clear = 1;
  830. }
  831. np->istat_sem = SEM;
  832. OUTB(np, nc_istat, SIGP|SEM);
  833. break;
  834. case UC_SETFLAG:
  835. tp->usrflags = uc->data;
  836. break;
  837. }
  838. }
  839. break;
  840. }
  841. }
  842. static int skip_spaces(char *ptr, int len)
  843. {
  844. int cnt, c;
  845. for (cnt = len; cnt > 0 && (c = *ptr++) && isspace(c); cnt--);
  846. return (len - cnt);
  847. }
  848. static int get_int_arg(char *ptr, int len, u_long *pv)
  849. {
  850. char *end;
  851. *pv = simple_strtoul(ptr, &end, 10);
  852. return (end - ptr);
  853. }
  854. static int is_keyword(char *ptr, int len, char *verb)
  855. {
  856. int verb_len = strlen(verb);
  857. if (len >= verb_len && !memcmp(verb, ptr, verb_len))
  858. return verb_len;
  859. else
  860. return 0;
  861. }
  862. #define SKIP_SPACES(ptr, len) \
  863. if ((arg_len = skip_spaces(ptr, len)) < 1) \
  864. return -EINVAL; \
  865. ptr += arg_len; len -= arg_len;
  866. #define GET_INT_ARG(ptr, len, v) \
  867. if (!(arg_len = get_int_arg(ptr, len, &(v)))) \
  868. return -EINVAL; \
  869. ptr += arg_len; len -= arg_len;
  870. /*
  871. * Parse a control command
  872. */
  873. static int sym_user_command(struct sym_hcb *np, char *buffer, int length)
  874. {
  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(np->s.host->host_lock, flags);
  993. sym_exec_user_command (np, uc);
  994. spin_unlock_irqrestore(np->s.host->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 sym_hcb *np, char *ptr, off_t offset, int len)
  1041. {
  1042. struct info_str info;
  1043. info.buffer = ptr;
  1044. info.length = len;
  1045. info.offset = offset;
  1046. info.pos = 0;
  1047. copy_info(&info, "Chip " NAME53C "%s, device id 0x%x, "
  1048. "revision id 0x%x\n", np->s.chip_name,
  1049. np->s.device->device, np->s.device->revision);
  1050. copy_info(&info, "At PCI address %s, IRQ " IRQ_FMT "\n",
  1051. pci_name(np->s.device), IRQ_PRM(np->s.device->irq));
  1052. copy_info(&info, "Min. period factor %d, %s SCSI BUS%s\n",
  1053. (int) (np->minsync_dt ? np->minsync_dt : np->minsync),
  1054. np->maxwide ? "Wide" : "Narrow",
  1055. np->minsync_dt ? ", DT capable" : "");
  1056. copy_info(&info, "Max. started commands %d, "
  1057. "max. commands per LUN %d\n",
  1058. SYM_CONF_MAX_START, SYM_CONF_MAX_TAG);
  1059. return info.pos > info.offset? info.pos - info.offset : 0;
  1060. }
  1061. #endif /* SYM_LINUX_USER_INFO_SUPPORT */
  1062. /*
  1063. * Entry point of the scsi proc fs of the driver.
  1064. * - func = 0 means read (returns adapter infos)
  1065. * - func = 1 means write (not yet merget from sym53c8xx)
  1066. */
  1067. static int sym53c8xx_proc_info(struct Scsi_Host *host, char *buffer,
  1068. char **start, off_t offset, int length, int func)
  1069. {
  1070. struct sym_hcb *np = sym_get_hcb(host);
  1071. int retv;
  1072. if (func) {
  1073. #ifdef SYM_LINUX_USER_COMMAND_SUPPORT
  1074. retv = sym_user_command(np, buffer, length);
  1075. #else
  1076. retv = -EINVAL;
  1077. #endif
  1078. } else {
  1079. if (start)
  1080. *start = buffer;
  1081. #ifdef SYM_LINUX_USER_INFO_SUPPORT
  1082. retv = sym_host_info(np, buffer, offset, length);
  1083. #else
  1084. retv = -EINVAL;
  1085. #endif
  1086. }
  1087. return retv;
  1088. }
  1089. #endif /* SYM_LINUX_PROC_INFO_SUPPORT */
  1090. /*
  1091. * Free controller resources.
  1092. */
  1093. static void sym_free_resources(struct sym_hcb *np, struct pci_dev *pdev)
  1094. {
  1095. /*
  1096. * Free O/S specific resources.
  1097. */
  1098. if (pdev->irq)
  1099. free_irq(pdev->irq, np);
  1100. if (np->s.ioaddr)
  1101. pci_iounmap(pdev, np->s.ioaddr);
  1102. if (np->s.ramaddr)
  1103. pci_iounmap(pdev, np->s.ramaddr);
  1104. /*
  1105. * Free O/S independent resources.
  1106. */
  1107. sym_hcb_free(np);
  1108. sym_mfree_dma(np, sizeof(*np), "HCB");
  1109. }
  1110. /*
  1111. * Host attach and initialisations.
  1112. *
  1113. * Allocate host data and ncb structure.
  1114. * Remap MMIO region.
  1115. * Do chip initialization.
  1116. * If all is OK, install interrupt handling and
  1117. * start the timer daemon.
  1118. */
  1119. static struct Scsi_Host * __devinit sym_attach(struct scsi_host_template *tpnt,
  1120. int unit, struct sym_device *dev)
  1121. {
  1122. struct host_data *host_data;
  1123. struct sym_hcb *np = NULL;
  1124. struct Scsi_Host *instance = NULL;
  1125. struct pci_dev *pdev = dev->pdev;
  1126. unsigned long flags;
  1127. struct sym_fw *fw;
  1128. printk(KERN_INFO "sym%d: <%s> rev 0x%x at pci %s irq " IRQ_FMT "\n",
  1129. unit, dev->chip.name, pdev->revision, pci_name(pdev),
  1130. IRQ_PRM(pdev->irq));
  1131. /*
  1132. * Get the firmware for this chip.
  1133. */
  1134. fw = sym_find_firmware(&dev->chip);
  1135. if (!fw)
  1136. goto attach_failed;
  1137. /*
  1138. * Allocate host_data structure
  1139. */
  1140. instance = scsi_host_alloc(tpnt, sizeof(*host_data));
  1141. if (!instance)
  1142. goto attach_failed;
  1143. host_data = (struct host_data *) instance->hostdata;
  1144. /*
  1145. * Allocate immediately the host control block,
  1146. * since we are only expecting to succeed. :)
  1147. * We keep track in the HCB of all the resources that
  1148. * are to be released on error.
  1149. */
  1150. np = __sym_calloc_dma(&pdev->dev, sizeof(*np), "HCB");
  1151. if (!np)
  1152. goto attach_failed;
  1153. np->s.device = pdev;
  1154. np->bus_dmat = &pdev->dev; /* Result in 1 DMA pool per HBA */
  1155. host_data->ncb = np;
  1156. np->s.host = instance;
  1157. pci_set_drvdata(pdev, np);
  1158. /*
  1159. * Copy some useful infos to the HCB.
  1160. */
  1161. np->hcb_ba = vtobus(np);
  1162. np->verbose = sym_driver_setup.verbose;
  1163. np->s.device = pdev;
  1164. np->s.unit = unit;
  1165. np->features = dev->chip.features;
  1166. np->clock_divn = dev->chip.nr_divisor;
  1167. np->maxoffs = dev->chip.offset_max;
  1168. np->maxburst = dev->chip.burst_max;
  1169. np->myaddr = dev->host_id;
  1170. /*
  1171. * Edit its name.
  1172. */
  1173. strlcpy(np->s.chip_name, dev->chip.name, sizeof(np->s.chip_name));
  1174. sprintf(np->s.inst_name, "sym%d", np->s.unit);
  1175. if ((SYM_CONF_DMA_ADDRESSING_MODE > 0) && (np->features & FE_DAC) &&
  1176. !pci_set_dma_mask(np->s.device, DMA_DAC_MASK)) {
  1177. set_dac(np);
  1178. } else if (pci_set_dma_mask(np->s.device, DMA_32BIT_MASK)) {
  1179. printf_warning("%s: No suitable DMA available\n", sym_name(np));
  1180. goto attach_failed;
  1181. }
  1182. /*
  1183. * Try to map the controller chip to
  1184. * virtual and physical memory.
  1185. */
  1186. np->mmio_ba = (u32)dev->mmio_base;
  1187. np->s.ioaddr = dev->s.ioaddr;
  1188. np->s.ramaddr = dev->s.ramaddr;
  1189. /*
  1190. * Map on-chip RAM if present and supported.
  1191. */
  1192. if (!(np->features & FE_RAM))
  1193. dev->ram_base = 0;
  1194. if (dev->ram_base)
  1195. np->ram_ba = (u32)dev->ram_base;
  1196. if (sym_hcb_attach(instance, fw, dev->nvram))
  1197. goto attach_failed;
  1198. /*
  1199. * Install the interrupt handler.
  1200. * If we synchonize the C code with SCRIPTS on interrupt,
  1201. * we do not want to share the INTR line at all.
  1202. */
  1203. if (request_irq(pdev->irq, sym53c8xx_intr, IRQF_SHARED, NAME53C8XX, np)) {
  1204. printf_err("%s: request irq %d failure\n",
  1205. sym_name(np), pdev->irq);
  1206. goto attach_failed;
  1207. }
  1208. /*
  1209. * After SCSI devices have been opened, we cannot
  1210. * reset the bus safely, so we do it here.
  1211. */
  1212. spin_lock_irqsave(instance->host_lock, flags);
  1213. if (sym_reset_scsi_bus(np, 0))
  1214. goto reset_failed;
  1215. /*
  1216. * Start the SCRIPTS.
  1217. */
  1218. sym_start_up(np, 1);
  1219. /*
  1220. * Start the timer daemon
  1221. */
  1222. init_timer(&np->s.timer);
  1223. np->s.timer.data = (unsigned long) np;
  1224. np->s.timer.function = sym53c8xx_timer;
  1225. np->s.lasttime=0;
  1226. sym_timer (np);
  1227. /*
  1228. * Fill Linux host instance structure
  1229. * and return success.
  1230. */
  1231. instance->max_channel = 0;
  1232. instance->this_id = np->myaddr;
  1233. instance->max_id = np->maxwide ? 16 : 8;
  1234. instance->max_lun = SYM_CONF_MAX_LUN;
  1235. instance->unique_id = pci_resource_start(pdev, 0);
  1236. instance->cmd_per_lun = SYM_CONF_MAX_TAG;
  1237. instance->can_queue = (SYM_CONF_MAX_START-2);
  1238. instance->sg_tablesize = SYM_CONF_MAX_SG;
  1239. instance->max_cmd_len = 16;
  1240. BUG_ON(sym2_transport_template == NULL);
  1241. instance->transportt = sym2_transport_template;
  1242. /* 53c896 rev 1 errata: DMA may not cross 16MB boundary */
  1243. if (pdev->device == PCI_DEVICE_ID_NCR_53C896 && pdev->revision < 2)
  1244. instance->dma_boundary = 0xFFFFFF;
  1245. spin_unlock_irqrestore(instance->host_lock, flags);
  1246. return instance;
  1247. reset_failed:
  1248. printf_err("%s: FATAL ERROR: CHECK SCSI BUS - CABLES, "
  1249. "TERMINATION, DEVICE POWER etc.!\n", sym_name(np));
  1250. spin_unlock_irqrestore(instance->host_lock, flags);
  1251. attach_failed:
  1252. if (!instance)
  1253. return NULL;
  1254. printf_info("%s: giving up ...\n", sym_name(np));
  1255. if (np)
  1256. sym_free_resources(np, pdev);
  1257. scsi_host_put(instance);
  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. * Read and check the PCI configuration for any detected NCR
  1368. * boards and save data for attaching after all boards have
  1369. * been detected.
  1370. */
  1371. static void __devinit
  1372. sym_init_device(struct pci_dev *pdev, struct sym_device *device)
  1373. {
  1374. int i = 2;
  1375. struct pci_bus_region bus_addr;
  1376. device->host_id = SYM_SETUP_HOST_ID;
  1377. device->pdev = pdev;
  1378. pcibios_resource_to_bus(pdev, &bus_addr, &pdev->resource[1]);
  1379. device->mmio_base = bus_addr.start;
  1380. /*
  1381. * If the BAR is 64-bit, resource 2 will be occupied by the
  1382. * upper 32 bits
  1383. */
  1384. if (!pdev->resource[i].flags)
  1385. i++;
  1386. pcibios_resource_to_bus(pdev, &bus_addr, &pdev->resource[i]);
  1387. device->ram_base = bus_addr.start;
  1388. #ifdef CONFIG_SCSI_SYM53C8XX_MMIO
  1389. if (device->mmio_base)
  1390. device->s.ioaddr = pci_iomap(pdev, 1,
  1391. pci_resource_len(pdev, 1));
  1392. #endif
  1393. if (!device->s.ioaddr)
  1394. device->s.ioaddr = pci_iomap(pdev, 0,
  1395. pci_resource_len(pdev, 0));
  1396. if (device->ram_base)
  1397. device->s.ramaddr = pci_iomap(pdev, i,
  1398. pci_resource_len(pdev, i));
  1399. }
  1400. /*
  1401. * The NCR PQS and PDS cards are constructed as a DEC bridge
  1402. * behind which sits a proprietary NCR memory controller and
  1403. * either four or two 53c875s as separate devices. We can tell
  1404. * if an 875 is part of a PQS/PDS or not since if it is, it will
  1405. * be on the same bus as the memory controller. In its usual
  1406. * mode of operation, the 875s are slaved to the memory
  1407. * controller for all transfers. To operate with the Linux
  1408. * driver, the memory controller is disabled and the 875s
  1409. * freed to function independently. The only wrinkle is that
  1410. * the preset SCSI ID (which may be zero) must be read in from
  1411. * a special configuration space register of the 875.
  1412. */
  1413. static void sym_config_pqs(struct pci_dev *pdev, struct sym_device *sym_dev)
  1414. {
  1415. int slot;
  1416. u8 tmp;
  1417. for (slot = 0; slot < 256; slot++) {
  1418. struct pci_dev *memc = pci_get_slot(pdev->bus, slot);
  1419. if (!memc || memc->vendor != 0x101a || memc->device == 0x0009) {
  1420. pci_dev_put(memc);
  1421. continue;
  1422. }
  1423. /* bit 1: allow individual 875 configuration */
  1424. pci_read_config_byte(memc, 0x44, &tmp);
  1425. if ((tmp & 0x2) == 0) {
  1426. tmp |= 0x2;
  1427. pci_write_config_byte(memc, 0x44, tmp);
  1428. }
  1429. /* bit 2: drive individual 875 interrupts to the bus */
  1430. pci_read_config_byte(memc, 0x45, &tmp);
  1431. if ((tmp & 0x4) == 0) {
  1432. tmp |= 0x4;
  1433. pci_write_config_byte(memc, 0x45, tmp);
  1434. }
  1435. pci_dev_put(memc);
  1436. break;
  1437. }
  1438. pci_read_config_byte(pdev, 0x84, &tmp);
  1439. sym_dev->host_id = tmp;
  1440. }
  1441. /*
  1442. * Called before unloading the module.
  1443. * Detach the host.
  1444. * We have to free resources and halt the NCR chip.
  1445. */
  1446. static int sym_detach(struct sym_hcb *np, struct pci_dev *pdev)
  1447. {
  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 *instance;
  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. instance = sym_attach(&sym2_template, attach_count, &sym_dev);
  1511. if (!instance)
  1512. goto free;
  1513. if (scsi_add_host(instance, &pdev->dev))
  1514. goto detach;
  1515. scsi_scan_host(instance);
  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 sym_hcb *np = pci_get_drvdata(pdev);
  1530. struct Scsi_Host *host = np->s.host;
  1531. scsi_remove_host(host);
  1532. scsi_host_put(host);
  1533. sym_detach(np, pdev);
  1534. pci_release_regions(pdev);
  1535. pci_disable_device(pdev);
  1536. attach_count--;
  1537. }
  1538. /**
  1539. * sym2_io_error_detected() - called when PCI error is detected
  1540. * @pdev: pointer to PCI device
  1541. * @state: current state of the PCI slot
  1542. */
  1543. static pci_ers_result_t sym2_io_error_detected(struct pci_dev *pdev,
  1544. enum pci_channel_state state)
  1545. {
  1546. /* If slot is permanently frozen, turn everything off */
  1547. if (state == pci_channel_io_perm_failure) {
  1548. sym2_remove(pdev);
  1549. return PCI_ERS_RESULT_DISCONNECT;
  1550. }
  1551. disable_irq(pdev->irq);
  1552. pci_disable_device(pdev);
  1553. /* Request that MMIO be enabled, so register dump can be taken. */
  1554. return PCI_ERS_RESULT_CAN_RECOVER;
  1555. }
  1556. /**
  1557. * sym2_io_slot_dump - Enable MMIO and dump debug registers
  1558. * @pdev: pointer to PCI device
  1559. */
  1560. static pci_ers_result_t sym2_io_slot_dump(struct pci_dev *pdev)
  1561. {
  1562. struct sym_hcb *np = pci_get_drvdata(pdev);
  1563. sym_dump_registers(np);
  1564. /* Request a slot reset. */
  1565. return PCI_ERS_RESULT_NEED_RESET;
  1566. }
  1567. /**
  1568. * sym2_reset_workarounds - hardware-specific work-arounds
  1569. *
  1570. * This routine is similar to sym_set_workarounds(), except
  1571. * that, at this point, we already know that the device was
  1572. * succesfully intialized at least once before, and so most
  1573. * of the steps taken there are un-needed here.
  1574. */
  1575. static void sym2_reset_workarounds(struct pci_dev *pdev)
  1576. {
  1577. u_short status_reg;
  1578. struct sym_chip *chip;
  1579. chip = sym_lookup_chip_table(pdev->device, pdev->revision);
  1580. /* Work around for errant bit in 895A, in a fashion
  1581. * similar to what is done in sym_set_workarounds().
  1582. */
  1583. pci_read_config_word(pdev, PCI_STATUS, &status_reg);
  1584. if (!(chip->features & FE_66MHZ) && (status_reg & PCI_STATUS_66MHZ)) {
  1585. status_reg = PCI_STATUS_66MHZ;
  1586. pci_write_config_word(pdev, PCI_STATUS, status_reg);
  1587. pci_read_config_word(pdev, PCI_STATUS, &status_reg);
  1588. }
  1589. }
  1590. /**
  1591. * sym2_io_slot_reset() - called when the pci bus has been reset.
  1592. * @pdev: pointer to PCI device
  1593. *
  1594. * Restart the card from scratch.
  1595. */
  1596. static pci_ers_result_t sym2_io_slot_reset(struct pci_dev *pdev)
  1597. {
  1598. struct sym_hcb *np = pci_get_drvdata(pdev);
  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(np, 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 sym_hcb *np = pci_get_drvdata(pdev);
  1637. struct Scsi_Host *shost = np->s.host;
  1638. struct host_data *hostdata = shost_priv(shost);
  1639. spin_lock_irq(shost->host_lock);
  1640. if (hostdata->io_reset)
  1641. complete_all(hostdata->io_reset);
  1642. hostdata->io_reset = NULL;
  1643. spin_unlock_irq(shost->host_lock);
  1644. }
  1645. static void sym2_get_signalling(struct Scsi_Host *shost)
  1646. {
  1647. struct sym_hcb *np = sym_get_hcb(shost);
  1648. enum spi_signal_type type;
  1649. switch (np->scsi_mode) {
  1650. case SMODE_SE:
  1651. type = SPI_SIGNAL_SE;
  1652. break;
  1653. case SMODE_LVD:
  1654. type = SPI_SIGNAL_LVD;
  1655. break;
  1656. case SMODE_HVD:
  1657. type = SPI_SIGNAL_HVD;
  1658. break;
  1659. default:
  1660. type = SPI_SIGNAL_UNKNOWN;
  1661. break;
  1662. }
  1663. spi_signalling(shost) = type;
  1664. }
  1665. static void sym2_set_offset(struct scsi_target *starget, int offset)
  1666. {
  1667. struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
  1668. struct sym_hcb *np = sym_get_hcb(shost);
  1669. struct sym_tcb *tp = &np->target[starget->id];
  1670. tp->tgoal.offset = offset;
  1671. tp->tgoal.check_nego = 1;
  1672. }
  1673. static void sym2_set_period(struct scsi_target *starget, int period)
  1674. {
  1675. struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
  1676. struct sym_hcb *np = sym_get_hcb(shost);
  1677. struct sym_tcb *tp = &np->target[starget->id];
  1678. /* have to have DT for these transfers, but DT will also
  1679. * set width, so check that this is allowed */
  1680. if (period <= np->minsync && spi_width(starget))
  1681. tp->tgoal.dt = 1;
  1682. tp->tgoal.period = period;
  1683. tp->tgoal.check_nego = 1;
  1684. }
  1685. static void sym2_set_width(struct scsi_target *starget, int width)
  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. /* It is illegal to have DT set on narrow transfers. If DT is
  1691. * clear, we must also clear IU and QAS. */
  1692. if (width == 0)
  1693. tp->tgoal.iu = tp->tgoal.dt = tp->tgoal.qas = 0;
  1694. tp->tgoal.width = width;
  1695. tp->tgoal.check_nego = 1;
  1696. }
  1697. static void sym2_set_dt(struct scsi_target *starget, int dt)
  1698. {
  1699. struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
  1700. struct sym_hcb *np = sym_get_hcb(shost);
  1701. struct sym_tcb *tp = &np->target[starget->id];
  1702. /* We must clear QAS and IU if DT is clear */
  1703. if (dt)
  1704. tp->tgoal.dt = 1;
  1705. else
  1706. tp->tgoal.iu = tp->tgoal.dt = tp->tgoal.qas = 0;
  1707. tp->tgoal.check_nego = 1;
  1708. }
  1709. #if 0
  1710. static void sym2_set_iu(struct scsi_target *starget, int iu)
  1711. {
  1712. struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
  1713. struct sym_hcb *np = sym_get_hcb(shost);
  1714. struct sym_tcb *tp = &np->target[starget->id];
  1715. if (iu)
  1716. tp->tgoal.iu = tp->tgoal.dt = 1;
  1717. else
  1718. tp->tgoal.iu = 0;
  1719. tp->tgoal.check_nego = 1;
  1720. }
  1721. static void sym2_set_qas(struct scsi_target *starget, int qas)
  1722. {
  1723. struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
  1724. struct sym_hcb *np = sym_get_hcb(shost);
  1725. struct sym_tcb *tp = &np->target[starget->id];
  1726. if (qas)
  1727. tp->tgoal.dt = tp->tgoal.qas = 1;
  1728. else
  1729. tp->tgoal.qas = 0;
  1730. tp->tgoal.check_nego = 1;
  1731. }
  1732. #endif
  1733. static struct spi_function_template sym2_transport_functions = {
  1734. .set_offset = sym2_set_offset,
  1735. .show_offset = 1,
  1736. .set_period = sym2_set_period,
  1737. .show_period = 1,
  1738. .set_width = sym2_set_width,
  1739. .show_width = 1,
  1740. .set_dt = sym2_set_dt,
  1741. .show_dt = 1,
  1742. #if 0
  1743. .set_iu = sym2_set_iu,
  1744. .show_iu = 1,
  1745. .set_qas = sym2_set_qas,
  1746. .show_qas = 1,
  1747. #endif
  1748. .get_signalling = sym2_get_signalling,
  1749. };
  1750. static struct pci_device_id sym2_id_table[] __devinitdata = {
  1751. { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_NCR_53C810,
  1752. PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0UL },
  1753. { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_NCR_53C820,
  1754. PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0UL }, /* new */
  1755. { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_NCR_53C825,
  1756. PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0UL },
  1757. { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_NCR_53C815,
  1758. PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0UL },
  1759. { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_53C810AP,
  1760. PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0UL }, /* new */
  1761. { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_NCR_53C860,
  1762. PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0UL },
  1763. { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_53C1510,
  1764. PCI_ANY_ID, PCI_ANY_ID, PCI_CLASS_STORAGE_SCSI<<8, 0xffff00, 0UL },
  1765. { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_NCR_53C896,
  1766. PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0UL },
  1767. { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_NCR_53C895,
  1768. PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0UL },
  1769. { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_NCR_53C885,
  1770. PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0UL },
  1771. { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_NCR_53C875,
  1772. PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0UL },
  1773. { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_NCR_53C1510,
  1774. PCI_ANY_ID, PCI_ANY_ID, PCI_CLASS_STORAGE_SCSI<<8, 0xffff00, 0UL }, /* new */
  1775. { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_53C895A,
  1776. PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0UL },
  1777. { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_53C875A,
  1778. PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0UL },
  1779. { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_53C1010_33,
  1780. PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0UL },
  1781. { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_53C1010_66,
  1782. PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0UL },
  1783. { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_NCR_53C875J,
  1784. PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0UL },
  1785. { 0, }
  1786. };
  1787. MODULE_DEVICE_TABLE(pci, sym2_id_table);
  1788. static struct pci_error_handlers sym2_err_handler = {
  1789. .error_detected = sym2_io_error_detected,
  1790. .mmio_enabled = sym2_io_slot_dump,
  1791. .slot_reset = sym2_io_slot_reset,
  1792. .resume = sym2_io_resume,
  1793. };
  1794. static struct pci_driver sym2_driver = {
  1795. .name = NAME53C8XX,
  1796. .id_table = sym2_id_table,
  1797. .probe = sym2_probe,
  1798. .remove = __devexit_p(sym2_remove),
  1799. .err_handler = &sym2_err_handler,
  1800. };
  1801. static int __init sym2_init(void)
  1802. {
  1803. int error;
  1804. sym2_setup_params();
  1805. sym2_transport_template = spi_attach_transport(&sym2_transport_functions);
  1806. if (!sym2_transport_template)
  1807. return -ENODEV;
  1808. error = pci_register_driver(&sym2_driver);
  1809. if (error)
  1810. spi_release_transport(sym2_transport_template);
  1811. return error;
  1812. }
  1813. static void __exit sym2_exit(void)
  1814. {
  1815. pci_unregister_driver(&sym2_driver);
  1816. spi_release_transport(sym2_transport_template);
  1817. }
  1818. module_init(sym2_init);
  1819. module_exit(sym2_exit);