sym_glue.c 53 KB

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