sym_glue.c 52 KB

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