sym_glue.c 53 KB

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