sym_glue.c 55 KB

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