sym_glue.c 53 KB

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