sym_glue.c 54 KB

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