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