sym_glue.c 54 KB

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