sym_glue.c 53 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118
  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. * Ask/tell the system about DMA addressing.
  1112. */
  1113. static int sym_setup_bus_dma_mask(struct sym_hcb *np)
  1114. {
  1115. #if SYM_CONF_DMA_ADDRESSING_MODE > 0
  1116. #if SYM_CONF_DMA_ADDRESSING_MODE == 1
  1117. #define DMA_DAC_MASK DMA_40BIT_MASK
  1118. #elif SYM_CONF_DMA_ADDRESSING_MODE == 2
  1119. #define DMA_DAC_MASK DMA_64BIT_MASK
  1120. #endif
  1121. if ((np->features & FE_DAC) &&
  1122. !pci_set_dma_mask(np->s.device, DMA_DAC_MASK)) {
  1123. np->use_dac = 1;
  1124. return 0;
  1125. }
  1126. #endif
  1127. if (!pci_set_dma_mask(np->s.device, DMA_32BIT_MASK))
  1128. return 0;
  1129. printf_warning("%s: No suitable DMA available\n", sym_name(np));
  1130. return -1;
  1131. }
  1132. /*
  1133. * Host attach and initialisations.
  1134. *
  1135. * Allocate host data and ncb structure.
  1136. * Remap MMIO region.
  1137. * Do chip initialization.
  1138. * If all is OK, install interrupt handling and
  1139. * start the timer daemon.
  1140. */
  1141. static struct Scsi_Host * __devinit sym_attach(struct scsi_host_template *tpnt,
  1142. int unit, struct sym_device *dev)
  1143. {
  1144. struct host_data *host_data;
  1145. struct sym_hcb *np = NULL;
  1146. struct Scsi_Host *instance = NULL;
  1147. struct pci_dev *pdev = dev->pdev;
  1148. unsigned long flags;
  1149. struct sym_fw *fw;
  1150. printk(KERN_INFO "sym%d: <%s> rev 0x%x at pci %s irq " IRQ_FMT "\n",
  1151. unit, dev->chip.name, pdev->revision, pci_name(pdev),
  1152. IRQ_PRM(pdev->irq));
  1153. /*
  1154. * Get the firmware for this chip.
  1155. */
  1156. fw = sym_find_firmware(&dev->chip);
  1157. if (!fw)
  1158. goto attach_failed;
  1159. /*
  1160. * Allocate host_data structure
  1161. */
  1162. instance = scsi_host_alloc(tpnt, sizeof(*host_data));
  1163. if (!instance)
  1164. goto attach_failed;
  1165. host_data = (struct host_data *) instance->hostdata;
  1166. /*
  1167. * Allocate immediately the host control block,
  1168. * since we are only expecting to succeed. :)
  1169. * We keep track in the HCB of all the resources that
  1170. * are to be released on error.
  1171. */
  1172. np = __sym_calloc_dma(&pdev->dev, sizeof(*np), "HCB");
  1173. if (!np)
  1174. goto attach_failed;
  1175. np->s.device = pdev;
  1176. np->bus_dmat = &pdev->dev; /* Result in 1 DMA pool per HBA */
  1177. host_data->ncb = np;
  1178. np->s.host = instance;
  1179. pci_set_drvdata(pdev, np);
  1180. /*
  1181. * Copy some useful infos to the HCB.
  1182. */
  1183. np->hcb_ba = vtobus(np);
  1184. np->verbose = sym_driver_setup.verbose;
  1185. np->s.device = pdev;
  1186. np->s.unit = unit;
  1187. np->features = dev->chip.features;
  1188. np->clock_divn = dev->chip.nr_divisor;
  1189. np->maxoffs = dev->chip.offset_max;
  1190. np->maxburst = dev->chip.burst_max;
  1191. np->myaddr = dev->host_id;
  1192. /*
  1193. * Edit its name.
  1194. */
  1195. strlcpy(np->s.chip_name, dev->chip.name, sizeof(np->s.chip_name));
  1196. sprintf(np->s.inst_name, "sym%d", np->s.unit);
  1197. if (sym_setup_bus_dma_mask(np))
  1198. goto attach_failed;
  1199. /*
  1200. * Try to map the controller chip to
  1201. * virtual and physical memory.
  1202. */
  1203. np->mmio_ba = (u32)dev->mmio_base;
  1204. np->s.ioaddr = dev->s.ioaddr;
  1205. np->s.ramaddr = dev->s.ramaddr;
  1206. /*
  1207. * Map on-chip RAM if present and supported.
  1208. */
  1209. if (!(np->features & FE_RAM))
  1210. dev->ram_base = 0;
  1211. if (dev->ram_base)
  1212. np->ram_ba = (u32)dev->ram_base;
  1213. if (sym_hcb_attach(instance, fw, dev->nvram))
  1214. goto attach_failed;
  1215. /*
  1216. * Install the interrupt handler.
  1217. * If we synchonize the C code with SCRIPTS on interrupt,
  1218. * we do not want to share the INTR line at all.
  1219. */
  1220. if (request_irq(pdev->irq, sym53c8xx_intr, IRQF_SHARED, NAME53C8XX, np)) {
  1221. printf_err("%s: request irq %d failure\n",
  1222. sym_name(np), pdev->irq);
  1223. goto attach_failed;
  1224. }
  1225. /*
  1226. * After SCSI devices have been opened, we cannot
  1227. * reset the bus safely, so we do it here.
  1228. */
  1229. spin_lock_irqsave(instance->host_lock, flags);
  1230. if (sym_reset_scsi_bus(np, 0))
  1231. goto reset_failed;
  1232. /*
  1233. * Start the SCRIPTS.
  1234. */
  1235. sym_start_up(np, 1);
  1236. /*
  1237. * Start the timer daemon
  1238. */
  1239. init_timer(&np->s.timer);
  1240. np->s.timer.data = (unsigned long) np;
  1241. np->s.timer.function = sym53c8xx_timer;
  1242. np->s.lasttime=0;
  1243. sym_timer (np);
  1244. /*
  1245. * Fill Linux host instance structure
  1246. * and return success.
  1247. */
  1248. instance->max_channel = 0;
  1249. instance->this_id = np->myaddr;
  1250. instance->max_id = np->maxwide ? 16 : 8;
  1251. instance->max_lun = SYM_CONF_MAX_LUN;
  1252. instance->unique_id = pci_resource_start(pdev, 0);
  1253. instance->cmd_per_lun = SYM_CONF_MAX_TAG;
  1254. instance->can_queue = (SYM_CONF_MAX_START-2);
  1255. instance->sg_tablesize = SYM_CONF_MAX_SG;
  1256. instance->max_cmd_len = 16;
  1257. BUG_ON(sym2_transport_template == NULL);
  1258. instance->transportt = sym2_transport_template;
  1259. /* 53c896 rev 1 errata: DMA may not cross 16MB boundary */
  1260. if (pdev->device == PCI_DEVICE_ID_NCR_53C896 && pdev->revision < 2)
  1261. instance->dma_boundary = 0xFFFFFF;
  1262. spin_unlock_irqrestore(instance->host_lock, flags);
  1263. return instance;
  1264. reset_failed:
  1265. printf_err("%s: FATAL ERROR: CHECK SCSI BUS - CABLES, "
  1266. "TERMINATION, DEVICE POWER etc.!\n", sym_name(np));
  1267. spin_unlock_irqrestore(instance->host_lock, flags);
  1268. attach_failed:
  1269. if (!instance)
  1270. return NULL;
  1271. printf_info("%s: giving up ...\n", sym_name(np));
  1272. if (np)
  1273. sym_free_resources(np, pdev);
  1274. scsi_host_put(instance);
  1275. return NULL;
  1276. }
  1277. /*
  1278. * Detect and try to read SYMBIOS and TEKRAM NVRAM.
  1279. */
  1280. #if SYM_CONF_NVRAM_SUPPORT
  1281. static void __devinit sym_get_nvram(struct sym_device *devp, struct sym_nvram *nvp)
  1282. {
  1283. devp->nvram = nvp;
  1284. nvp->type = 0;
  1285. sym_read_nvram(devp, nvp);
  1286. }
  1287. #else
  1288. static inline void sym_get_nvram(struct sym_device *devp, struct sym_nvram *nvp)
  1289. {
  1290. }
  1291. #endif /* SYM_CONF_NVRAM_SUPPORT */
  1292. static int __devinit sym_check_supported(struct sym_device *device)
  1293. {
  1294. struct sym_chip *chip;
  1295. struct pci_dev *pdev = device->pdev;
  1296. unsigned long io_port = pci_resource_start(pdev, 0);
  1297. int i;
  1298. /*
  1299. * If user excluded this chip, do not initialize it.
  1300. * I hate this code so much. Must kill it.
  1301. */
  1302. if (io_port) {
  1303. for (i = 0 ; i < 8 ; i++) {
  1304. if (sym_driver_setup.excludes[i] == io_port)
  1305. return -ENODEV;
  1306. }
  1307. }
  1308. /*
  1309. * Check if the chip is supported. Then copy the chip description
  1310. * to our device structure so we can make it match the actual device
  1311. * and options.
  1312. */
  1313. chip = sym_lookup_chip_table(pdev->device, pdev->revision);
  1314. if (!chip) {
  1315. dev_info(&pdev->dev, "device not supported\n");
  1316. return -ENODEV;
  1317. }
  1318. memcpy(&device->chip, chip, sizeof(device->chip));
  1319. return 0;
  1320. }
  1321. /*
  1322. * Ignore Symbios chips controlled by various RAID controllers.
  1323. * These controllers set value 0x52414944 at RAM end - 16.
  1324. */
  1325. static int __devinit sym_check_raid(struct sym_device *device)
  1326. {
  1327. unsigned int ram_size, ram_val;
  1328. if (!device->s.ramaddr)
  1329. return 0;
  1330. if (device->chip.features & FE_RAM8K)
  1331. ram_size = 8192;
  1332. else
  1333. ram_size = 4096;
  1334. ram_val = readl(device->s.ramaddr + ram_size - 16);
  1335. if (ram_val != 0x52414944)
  1336. return 0;
  1337. dev_info(&device->pdev->dev,
  1338. "not initializing, driven by RAID controller.\n");
  1339. return -ENODEV;
  1340. }
  1341. static int __devinit sym_set_workarounds(struct sym_device *device)
  1342. {
  1343. struct sym_chip *chip = &device->chip;
  1344. struct pci_dev *pdev = device->pdev;
  1345. u_short status_reg;
  1346. /*
  1347. * (ITEM 12 of a DEL about the 896 I haven't yet).
  1348. * We must ensure the chip will use WRITE AND INVALIDATE.
  1349. * The revision number limit is for now arbitrary.
  1350. */
  1351. if (pdev->device == PCI_DEVICE_ID_NCR_53C896 && pdev->revision < 0x4) {
  1352. chip->features |= (FE_WRIE | FE_CLSE);
  1353. }
  1354. /* If the chip can do Memory Write Invalidate, enable it */
  1355. if (chip->features & FE_WRIE) {
  1356. if (pci_set_mwi(pdev))
  1357. return -ENODEV;
  1358. }
  1359. /*
  1360. * Work around for errant bit in 895A. The 66Mhz
  1361. * capable bit is set erroneously. Clear this bit.
  1362. * (Item 1 DEL 533)
  1363. *
  1364. * Make sure Config space and Features agree.
  1365. *
  1366. * Recall: writes are not normal to status register -
  1367. * write a 1 to clear and a 0 to leave unchanged.
  1368. * Can only reset bits.
  1369. */
  1370. pci_read_config_word(pdev, PCI_STATUS, &status_reg);
  1371. if (chip->features & FE_66MHZ) {
  1372. if (!(status_reg & PCI_STATUS_66MHZ))
  1373. chip->features &= ~FE_66MHZ;
  1374. } else {
  1375. if (status_reg & PCI_STATUS_66MHZ) {
  1376. status_reg = PCI_STATUS_66MHZ;
  1377. pci_write_config_word(pdev, PCI_STATUS, status_reg);
  1378. pci_read_config_word(pdev, PCI_STATUS, &status_reg);
  1379. }
  1380. }
  1381. return 0;
  1382. }
  1383. /*
  1384. * Read and check the PCI configuration for any detected NCR
  1385. * boards and save data for attaching after all boards have
  1386. * been detected.
  1387. */
  1388. static void __devinit
  1389. sym_init_device(struct pci_dev *pdev, struct sym_device *device)
  1390. {
  1391. int i = 2;
  1392. struct pci_bus_region bus_addr;
  1393. device->host_id = SYM_SETUP_HOST_ID;
  1394. device->pdev = pdev;
  1395. pcibios_resource_to_bus(pdev, &bus_addr, &pdev->resource[1]);
  1396. device->mmio_base = bus_addr.start;
  1397. /*
  1398. * If the BAR is 64-bit, resource 2 will be occupied by the
  1399. * upper 32 bits
  1400. */
  1401. if (!pdev->resource[i].flags)
  1402. i++;
  1403. pcibios_resource_to_bus(pdev, &bus_addr, &pdev->resource[i]);
  1404. device->ram_base = bus_addr.start;
  1405. #ifdef CONFIG_SCSI_SYM53C8XX_MMIO
  1406. if (device->mmio_base)
  1407. device->s.ioaddr = pci_iomap(pdev, 1,
  1408. pci_resource_len(pdev, 1));
  1409. #endif
  1410. if (!device->s.ioaddr)
  1411. device->s.ioaddr = pci_iomap(pdev, 0,
  1412. pci_resource_len(pdev, 0));
  1413. if (device->ram_base)
  1414. device->s.ramaddr = pci_iomap(pdev, i,
  1415. pci_resource_len(pdev, i));
  1416. }
  1417. /*
  1418. * The NCR PQS and PDS cards are constructed as a DEC bridge
  1419. * behind which sits a proprietary NCR memory controller and
  1420. * either four or two 53c875s as separate devices. We can tell
  1421. * if an 875 is part of a PQS/PDS or not since if it is, it will
  1422. * be on the same bus as the memory controller. In its usual
  1423. * mode of operation, the 875s are slaved to the memory
  1424. * controller for all transfers. To operate with the Linux
  1425. * driver, the memory controller is disabled and the 875s
  1426. * freed to function independently. The only wrinkle is that
  1427. * the preset SCSI ID (which may be zero) must be read in from
  1428. * a special configuration space register of the 875.
  1429. */
  1430. static void sym_config_pqs(struct pci_dev *pdev, struct sym_device *sym_dev)
  1431. {
  1432. int slot;
  1433. u8 tmp;
  1434. for (slot = 0; slot < 256; slot++) {
  1435. struct pci_dev *memc = pci_get_slot(pdev->bus, slot);
  1436. if (!memc || memc->vendor != 0x101a || memc->device == 0x0009) {
  1437. pci_dev_put(memc);
  1438. continue;
  1439. }
  1440. /* bit 1: allow individual 875 configuration */
  1441. pci_read_config_byte(memc, 0x44, &tmp);
  1442. if ((tmp & 0x2) == 0) {
  1443. tmp |= 0x2;
  1444. pci_write_config_byte(memc, 0x44, tmp);
  1445. }
  1446. /* bit 2: drive individual 875 interrupts to the bus */
  1447. pci_read_config_byte(memc, 0x45, &tmp);
  1448. if ((tmp & 0x4) == 0) {
  1449. tmp |= 0x4;
  1450. pci_write_config_byte(memc, 0x45, tmp);
  1451. }
  1452. pci_dev_put(memc);
  1453. break;
  1454. }
  1455. pci_read_config_byte(pdev, 0x84, &tmp);
  1456. sym_dev->host_id = tmp;
  1457. }
  1458. /*
  1459. * Called before unloading the module.
  1460. * Detach the host.
  1461. * We have to free resources and halt the NCR chip.
  1462. */
  1463. static int sym_detach(struct sym_hcb *np, struct pci_dev *pdev)
  1464. {
  1465. printk("%s: detaching ...\n", sym_name(np));
  1466. del_timer_sync(&np->s.timer);
  1467. /*
  1468. * Reset NCR chip.
  1469. * We should use sym_soft_reset(), but we don't want to do
  1470. * so, since we may not be safe if interrupts occur.
  1471. */
  1472. printk("%s: resetting chip\n", sym_name(np));
  1473. OUTB(np, nc_istat, SRST);
  1474. INB(np, nc_mbox1);
  1475. udelay(10);
  1476. OUTB(np, nc_istat, 0);
  1477. sym_free_resources(np, pdev);
  1478. return 1;
  1479. }
  1480. /*
  1481. * Driver host template.
  1482. */
  1483. static struct scsi_host_template sym2_template = {
  1484. .module = THIS_MODULE,
  1485. .name = "sym53c8xx",
  1486. .info = sym53c8xx_info,
  1487. .queuecommand = sym53c8xx_queue_command,
  1488. .slave_alloc = sym53c8xx_slave_alloc,
  1489. .slave_configure = sym53c8xx_slave_configure,
  1490. .slave_destroy = sym53c8xx_slave_destroy,
  1491. .eh_abort_handler = sym53c8xx_eh_abort_handler,
  1492. .eh_device_reset_handler = sym53c8xx_eh_device_reset_handler,
  1493. .eh_bus_reset_handler = sym53c8xx_eh_bus_reset_handler,
  1494. .eh_host_reset_handler = sym53c8xx_eh_host_reset_handler,
  1495. .this_id = 7,
  1496. .use_clustering = ENABLE_CLUSTERING,
  1497. .use_sg_chaining = ENABLE_SG_CHAINING,
  1498. .max_sectors = 0xFFFF,
  1499. #ifdef SYM_LINUX_PROC_INFO_SUPPORT
  1500. .proc_info = sym53c8xx_proc_info,
  1501. .proc_name = NAME53C8XX,
  1502. #endif
  1503. };
  1504. static int attach_count;
  1505. static int __devinit sym2_probe(struct pci_dev *pdev,
  1506. const struct pci_device_id *ent)
  1507. {
  1508. struct sym_device sym_dev;
  1509. struct sym_nvram nvram;
  1510. struct Scsi_Host *instance;
  1511. memset(&sym_dev, 0, sizeof(sym_dev));
  1512. memset(&nvram, 0, sizeof(nvram));
  1513. if (pci_enable_device(pdev))
  1514. goto leave;
  1515. pci_set_master(pdev);
  1516. if (pci_request_regions(pdev, NAME53C8XX))
  1517. goto disable;
  1518. sym_init_device(pdev, &sym_dev);
  1519. if (sym_check_supported(&sym_dev))
  1520. goto free;
  1521. if (sym_check_raid(&sym_dev))
  1522. goto leave; /* Don't disable the device */
  1523. if (sym_set_workarounds(&sym_dev))
  1524. goto free;
  1525. sym_config_pqs(pdev, &sym_dev);
  1526. sym_get_nvram(&sym_dev, &nvram);
  1527. instance = sym_attach(&sym2_template, attach_count, &sym_dev);
  1528. if (!instance)
  1529. goto free;
  1530. if (scsi_add_host(instance, &pdev->dev))
  1531. goto detach;
  1532. scsi_scan_host(instance);
  1533. attach_count++;
  1534. return 0;
  1535. detach:
  1536. sym_detach(pci_get_drvdata(pdev), pdev);
  1537. free:
  1538. pci_release_regions(pdev);
  1539. disable:
  1540. pci_disable_device(pdev);
  1541. leave:
  1542. return -ENODEV;
  1543. }
  1544. static void __devexit sym2_remove(struct pci_dev *pdev)
  1545. {
  1546. struct sym_hcb *np = pci_get_drvdata(pdev);
  1547. struct Scsi_Host *host = np->s.host;
  1548. scsi_remove_host(host);
  1549. scsi_host_put(host);
  1550. sym_detach(np, pdev);
  1551. pci_release_regions(pdev);
  1552. pci_disable_device(pdev);
  1553. attach_count--;
  1554. }
  1555. /**
  1556. * sym2_io_error_detected() - called when PCI error is detected
  1557. * @pdev: pointer to PCI device
  1558. * @state: current state of the PCI slot
  1559. */
  1560. static pci_ers_result_t sym2_io_error_detected(struct pci_dev *pdev,
  1561. enum pci_channel_state state)
  1562. {
  1563. /* If slot is permanently frozen, turn everything off */
  1564. if (state == pci_channel_io_perm_failure) {
  1565. sym2_remove(pdev);
  1566. return PCI_ERS_RESULT_DISCONNECT;
  1567. }
  1568. disable_irq(pdev->irq);
  1569. pci_disable_device(pdev);
  1570. /* Request that MMIO be enabled, so register dump can be taken. */
  1571. return PCI_ERS_RESULT_CAN_RECOVER;
  1572. }
  1573. /**
  1574. * sym2_io_slot_dump - Enable MMIO and dump debug registers
  1575. * @pdev: pointer to PCI device
  1576. */
  1577. static pci_ers_result_t sym2_io_slot_dump(struct pci_dev *pdev)
  1578. {
  1579. struct sym_hcb *np = pci_get_drvdata(pdev);
  1580. sym_dump_registers(np);
  1581. /* Request a slot reset. */
  1582. return PCI_ERS_RESULT_NEED_RESET;
  1583. }
  1584. /**
  1585. * sym2_reset_workarounds - hardware-specific work-arounds
  1586. *
  1587. * This routine is similar to sym_set_workarounds(), except
  1588. * that, at this point, we already know that the device was
  1589. * succesfully intialized at least once before, and so most
  1590. * of the steps taken there are un-needed here.
  1591. */
  1592. static void sym2_reset_workarounds(struct pci_dev *pdev)
  1593. {
  1594. u_short status_reg;
  1595. struct sym_chip *chip;
  1596. chip = sym_lookup_chip_table(pdev->device, pdev->revision);
  1597. /* Work around for errant bit in 895A, in a fashion
  1598. * similar to what is done in sym_set_workarounds().
  1599. */
  1600. pci_read_config_word(pdev, PCI_STATUS, &status_reg);
  1601. if (!(chip->features & FE_66MHZ) && (status_reg & PCI_STATUS_66MHZ)) {
  1602. status_reg = PCI_STATUS_66MHZ;
  1603. pci_write_config_word(pdev, PCI_STATUS, status_reg);
  1604. pci_read_config_word(pdev, PCI_STATUS, &status_reg);
  1605. }
  1606. }
  1607. /**
  1608. * sym2_io_slot_reset() - called when the pci bus has been reset.
  1609. * @pdev: pointer to PCI device
  1610. *
  1611. * Restart the card from scratch.
  1612. */
  1613. static pci_ers_result_t sym2_io_slot_reset(struct pci_dev *pdev)
  1614. {
  1615. struct sym_hcb *np = pci_get_drvdata(pdev);
  1616. printk(KERN_INFO "%s: recovering from a PCI slot reset\n",
  1617. sym_name(np));
  1618. if (pci_enable_device(pdev)) {
  1619. printk(KERN_ERR "%s: Unable to enable after PCI reset\n",
  1620. sym_name(np));
  1621. return PCI_ERS_RESULT_DISCONNECT;
  1622. }
  1623. pci_set_master(pdev);
  1624. enable_irq(pdev->irq);
  1625. /* If the chip can do Memory Write Invalidate, enable it */
  1626. if (np->features & FE_WRIE) {
  1627. if (pci_set_mwi(pdev))
  1628. return PCI_ERS_RESULT_DISCONNECT;
  1629. }
  1630. /* Perform work-arounds, analogous to sym_set_workarounds() */
  1631. sym2_reset_workarounds(pdev);
  1632. /* Perform host reset only on one instance of the card */
  1633. if (PCI_FUNC(pdev->devfn) == 0) {
  1634. if (sym_reset_scsi_bus(np, 0)) {
  1635. printk(KERN_ERR "%s: Unable to reset scsi host\n",
  1636. sym_name(np));
  1637. return PCI_ERS_RESULT_DISCONNECT;
  1638. }
  1639. sym_start_up(np, 1);
  1640. }
  1641. return PCI_ERS_RESULT_RECOVERED;
  1642. }
  1643. /**
  1644. * sym2_io_resume() - resume normal ops after PCI reset
  1645. * @pdev: pointer to PCI device
  1646. *
  1647. * Called when the error recovery driver tells us that its
  1648. * OK to resume normal operation. Use completion to allow
  1649. * halted scsi ops to resume.
  1650. */
  1651. static void sym2_io_resume(struct pci_dev *pdev)
  1652. {
  1653. struct sym_hcb *np = pci_get_drvdata(pdev);
  1654. struct Scsi_Host *shost = np->s.host;
  1655. struct host_data *hostdata = shost_priv(shost);
  1656. spin_lock_irq(shost->host_lock);
  1657. if (hostdata->io_reset)
  1658. complete_all(hostdata->io_reset);
  1659. hostdata->io_reset = NULL;
  1660. spin_unlock_irq(shost->host_lock);
  1661. }
  1662. static void sym2_get_signalling(struct Scsi_Host *shost)
  1663. {
  1664. struct sym_hcb *np = sym_get_hcb(shost);
  1665. enum spi_signal_type type;
  1666. switch (np->scsi_mode) {
  1667. case SMODE_SE:
  1668. type = SPI_SIGNAL_SE;
  1669. break;
  1670. case SMODE_LVD:
  1671. type = SPI_SIGNAL_LVD;
  1672. break;
  1673. case SMODE_HVD:
  1674. type = SPI_SIGNAL_HVD;
  1675. break;
  1676. default:
  1677. type = SPI_SIGNAL_UNKNOWN;
  1678. break;
  1679. }
  1680. spi_signalling(shost) = type;
  1681. }
  1682. static void sym2_set_offset(struct scsi_target *starget, int offset)
  1683. {
  1684. struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
  1685. struct sym_hcb *np = sym_get_hcb(shost);
  1686. struct sym_tcb *tp = &np->target[starget->id];
  1687. tp->tgoal.offset = offset;
  1688. tp->tgoal.check_nego = 1;
  1689. }
  1690. static void sym2_set_period(struct scsi_target *starget, int period)
  1691. {
  1692. struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
  1693. struct sym_hcb *np = sym_get_hcb(shost);
  1694. struct sym_tcb *tp = &np->target[starget->id];
  1695. /* have to have DT for these transfers, but DT will also
  1696. * set width, so check that this is allowed */
  1697. if (period <= np->minsync && spi_width(starget))
  1698. tp->tgoal.dt = 1;
  1699. tp->tgoal.period = period;
  1700. tp->tgoal.check_nego = 1;
  1701. }
  1702. static void sym2_set_width(struct scsi_target *starget, int width)
  1703. {
  1704. struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
  1705. struct sym_hcb *np = sym_get_hcb(shost);
  1706. struct sym_tcb *tp = &np->target[starget->id];
  1707. /* It is illegal to have DT set on narrow transfers. If DT is
  1708. * clear, we must also clear IU and QAS. */
  1709. if (width == 0)
  1710. tp->tgoal.iu = tp->tgoal.dt = tp->tgoal.qas = 0;
  1711. tp->tgoal.width = width;
  1712. tp->tgoal.check_nego = 1;
  1713. }
  1714. static void sym2_set_dt(struct scsi_target *starget, int dt)
  1715. {
  1716. struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
  1717. struct sym_hcb *np = sym_get_hcb(shost);
  1718. struct sym_tcb *tp = &np->target[starget->id];
  1719. /* We must clear QAS and IU if DT is clear */
  1720. if (dt)
  1721. tp->tgoal.dt = 1;
  1722. else
  1723. tp->tgoal.iu = tp->tgoal.dt = tp->tgoal.qas = 0;
  1724. tp->tgoal.check_nego = 1;
  1725. }
  1726. #if 0
  1727. static void sym2_set_iu(struct scsi_target *starget, int iu)
  1728. {
  1729. struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
  1730. struct sym_hcb *np = sym_get_hcb(shost);
  1731. struct sym_tcb *tp = &np->target[starget->id];
  1732. if (iu)
  1733. tp->tgoal.iu = tp->tgoal.dt = 1;
  1734. else
  1735. tp->tgoal.iu = 0;
  1736. tp->tgoal.check_nego = 1;
  1737. }
  1738. static void sym2_set_qas(struct scsi_target *starget, int qas)
  1739. {
  1740. struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
  1741. struct sym_hcb *np = sym_get_hcb(shost);
  1742. struct sym_tcb *tp = &np->target[starget->id];
  1743. if (qas)
  1744. tp->tgoal.dt = tp->tgoal.qas = 1;
  1745. else
  1746. tp->tgoal.qas = 0;
  1747. tp->tgoal.check_nego = 1;
  1748. }
  1749. #endif
  1750. static struct spi_function_template sym2_transport_functions = {
  1751. .set_offset = sym2_set_offset,
  1752. .show_offset = 1,
  1753. .set_period = sym2_set_period,
  1754. .show_period = 1,
  1755. .set_width = sym2_set_width,
  1756. .show_width = 1,
  1757. .set_dt = sym2_set_dt,
  1758. .show_dt = 1,
  1759. #if 0
  1760. .set_iu = sym2_set_iu,
  1761. .show_iu = 1,
  1762. .set_qas = sym2_set_qas,
  1763. .show_qas = 1,
  1764. #endif
  1765. .get_signalling = sym2_get_signalling,
  1766. };
  1767. static struct pci_device_id sym2_id_table[] __devinitdata = {
  1768. { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_NCR_53C810,
  1769. PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0UL },
  1770. { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_NCR_53C820,
  1771. PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0UL }, /* new */
  1772. { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_NCR_53C825,
  1773. PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0UL },
  1774. { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_NCR_53C815,
  1775. PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0UL },
  1776. { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_53C810AP,
  1777. PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0UL }, /* new */
  1778. { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_NCR_53C860,
  1779. PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0UL },
  1780. { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_53C1510,
  1781. PCI_ANY_ID, PCI_ANY_ID, PCI_CLASS_STORAGE_SCSI<<8, 0xffff00, 0UL },
  1782. { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_NCR_53C896,
  1783. PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0UL },
  1784. { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_NCR_53C895,
  1785. PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0UL },
  1786. { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_NCR_53C885,
  1787. PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0UL },
  1788. { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_NCR_53C875,
  1789. PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0UL },
  1790. { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_NCR_53C1510,
  1791. PCI_ANY_ID, PCI_ANY_ID, PCI_CLASS_STORAGE_SCSI<<8, 0xffff00, 0UL }, /* new */
  1792. { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_53C895A,
  1793. PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0UL },
  1794. { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_53C875A,
  1795. PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0UL },
  1796. { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_53C1010_33,
  1797. PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0UL },
  1798. { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_53C1010_66,
  1799. PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0UL },
  1800. { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_NCR_53C875J,
  1801. PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0UL },
  1802. { 0, }
  1803. };
  1804. MODULE_DEVICE_TABLE(pci, sym2_id_table);
  1805. static struct pci_error_handlers sym2_err_handler = {
  1806. .error_detected = sym2_io_error_detected,
  1807. .mmio_enabled = sym2_io_slot_dump,
  1808. .slot_reset = sym2_io_slot_reset,
  1809. .resume = sym2_io_resume,
  1810. };
  1811. static struct pci_driver sym2_driver = {
  1812. .name = NAME53C8XX,
  1813. .id_table = sym2_id_table,
  1814. .probe = sym2_probe,
  1815. .remove = __devexit_p(sym2_remove),
  1816. .err_handler = &sym2_err_handler,
  1817. };
  1818. static int __init sym2_init(void)
  1819. {
  1820. int error;
  1821. sym2_setup_params();
  1822. sym2_transport_template = spi_attach_transport(&sym2_transport_functions);
  1823. if (!sym2_transport_template)
  1824. return -ENODEV;
  1825. error = pci_register_driver(&sym2_driver);
  1826. if (error)
  1827. spi_release_transport(sym2_transport_template);
  1828. return error;
  1829. }
  1830. static void __exit sym2_exit(void)
  1831. {
  1832. pci_unregister_driver(&sym2_driver);
  1833. spi_release_transport(sym2_transport_template);
  1834. }
  1835. module_init(sym2_init);
  1836. module_exit(sym2_exit);