sym_glue.c 53 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144
  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. struct completion done;
  132. void (*old_done)(struct scsi_cmnd *);
  133. dma_addr_t data_mapping;
  134. int to_do;
  135. u_char data_mapped; /* corresponds to data_mapping above */
  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->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. dev_warn(&cmd->device->sdev_gendev, "%s operation started.\n", opname);
  644. spin_lock_irq(host->host_lock);
  645. /* This one is queued in some place -> to wait for completion */
  646. FOR_EACH_QUEUED_ELEMENT(&np->busy_ccbq, qp) {
  647. struct sym_ccb *cp = sym_que_entry(qp, struct sym_ccb, link_ccbq);
  648. if (cp->cmd == cmd) {
  649. to_do = SYM_EH_DO_WAIT;
  650. break;
  651. }
  652. }
  653. if (to_do == SYM_EH_DO_WAIT) {
  654. init_completion(&ucmd->done);
  655. ucmd->old_done = cmd->scsi_done;
  656. cmd->scsi_done = sym_eh_done;
  657. }
  658. /* Try to proceed the operation we have been asked for */
  659. sts = -1;
  660. switch(op) {
  661. case SYM_EH_ABORT:
  662. sts = sym_abort_scsiio(np, cmd, 1);
  663. break;
  664. case SYM_EH_DEVICE_RESET:
  665. sts = sym_reset_scsi_target(np, cmd->device->id);
  666. break;
  667. case SYM_EH_BUS_RESET:
  668. sym_reset_scsi_bus(np, 1);
  669. sts = 0;
  670. break;
  671. case SYM_EH_HOST_RESET:
  672. sym_reset_scsi_bus(np, 0);
  673. sym_start_up (np, 1);
  674. sts = 0;
  675. break;
  676. default:
  677. break;
  678. }
  679. /* On error, restore everything and cross fingers :) */
  680. if (sts) {
  681. cmd->scsi_done = ucmd->old_done;
  682. to_do = SYM_EH_DO_IGNORE;
  683. }
  684. ucmd->to_do = to_do;
  685. spin_unlock_irq(host->host_lock);
  686. if (to_do == SYM_EH_DO_WAIT) {
  687. if (!wait_for_completion_timeout(&ucmd->done, 5*HZ)) {
  688. ucmd->to_do = SYM_EH_DO_IGNORE;
  689. sts = -2;
  690. }
  691. }
  692. dev_warn(&cmd->device->sdev_gendev, "%s operation %s.\n", opname,
  693. sts==0 ? "complete" :sts==-2 ? "timed-out" : "failed");
  694. return sts ? SCSI_FAILED : SCSI_SUCCESS;
  695. }
  696. /*
  697. * Error handlers called from the eh thread (one thread per HBA).
  698. */
  699. static int sym53c8xx_eh_abort_handler(struct scsi_cmnd *cmd)
  700. {
  701. return sym_eh_handler(SYM_EH_ABORT, "ABORT", cmd);
  702. }
  703. static int sym53c8xx_eh_device_reset_handler(struct scsi_cmnd *cmd)
  704. {
  705. return sym_eh_handler(SYM_EH_DEVICE_RESET, "DEVICE RESET", cmd);
  706. }
  707. static int sym53c8xx_eh_bus_reset_handler(struct scsi_cmnd *cmd)
  708. {
  709. return sym_eh_handler(SYM_EH_BUS_RESET, "BUS RESET", cmd);
  710. }
  711. static int sym53c8xx_eh_host_reset_handler(struct scsi_cmnd *cmd)
  712. {
  713. return sym_eh_handler(SYM_EH_HOST_RESET, "HOST RESET", cmd);
  714. }
  715. /*
  716. * Tune device queuing depth, according to various limits.
  717. */
  718. static void sym_tune_dev_queuing(struct sym_tcb *tp, int lun, u_short reqtags)
  719. {
  720. struct sym_lcb *lp = sym_lp(tp, lun);
  721. u_short oldtags;
  722. if (!lp)
  723. return;
  724. oldtags = lp->s.reqtags;
  725. if (reqtags > lp->s.scdev_depth)
  726. reqtags = lp->s.scdev_depth;
  727. lp->s.reqtags = reqtags;
  728. if (reqtags != oldtags) {
  729. dev_info(&tp->starget->dev,
  730. "tagged command queuing %s, command queue depth %d.\n",
  731. lp->s.reqtags ? "enabled" : "disabled", reqtags);
  732. }
  733. }
  734. /*
  735. * Linux select queue depths function
  736. */
  737. #define DEF_DEPTH (sym_driver_setup.max_tag)
  738. #define ALL_TARGETS -2
  739. #define NO_TARGET -1
  740. #define ALL_LUNS -2
  741. #define NO_LUN -1
  742. static int device_queue_depth(struct sym_hcb *np, int target, int lun)
  743. {
  744. int c, h, t, u, v;
  745. char *p = sym_driver_setup.tag_ctrl;
  746. char *ep;
  747. h = -1;
  748. t = NO_TARGET;
  749. u = NO_LUN;
  750. while ((c = *p++) != 0) {
  751. v = simple_strtoul(p, &ep, 0);
  752. switch(c) {
  753. case '/':
  754. ++h;
  755. t = ALL_TARGETS;
  756. u = ALL_LUNS;
  757. break;
  758. case 't':
  759. if (t != target)
  760. t = (target == v) ? v : NO_TARGET;
  761. u = ALL_LUNS;
  762. break;
  763. case 'u':
  764. if (u != lun)
  765. u = (lun == v) ? v : NO_LUN;
  766. break;
  767. case 'q':
  768. if (h == np->s.unit &&
  769. (t == ALL_TARGETS || t == target) &&
  770. (u == ALL_LUNS || u == lun))
  771. return v;
  772. break;
  773. case '-':
  774. t = ALL_TARGETS;
  775. u = ALL_LUNS;
  776. break;
  777. default:
  778. break;
  779. }
  780. p = ep;
  781. }
  782. return DEF_DEPTH;
  783. }
  784. static int sym53c8xx_slave_alloc(struct scsi_device *sdev)
  785. {
  786. struct sym_hcb *np = sym_get_hcb(sdev->host);
  787. struct sym_tcb *tp = &np->target[sdev->id];
  788. struct sym_lcb *lp;
  789. if (sdev->id >= SYM_CONF_MAX_TARGET || sdev->lun >= SYM_CONF_MAX_LUN)
  790. return -ENXIO;
  791. tp->starget = sdev->sdev_target;
  792. /*
  793. * Fail the device init if the device is flagged NOSCAN at BOOT in
  794. * the NVRAM. This may speed up boot and maintain coherency with
  795. * BIOS device numbering. Clearing the flag allows the user to
  796. * rescan skipped devices later. We also return an error for
  797. * devices not flagged for SCAN LUNS in the NVRAM since some single
  798. * lun devices behave badly when asked for a non zero LUN.
  799. */
  800. if (tp->usrflags & SYM_SCAN_BOOT_DISABLED) {
  801. tp->usrflags &= ~SYM_SCAN_BOOT_DISABLED;
  802. starget_printk(KERN_INFO, tp->starget,
  803. "Scan at boot disabled in NVRAM\n");
  804. return -ENXIO;
  805. }
  806. if (tp->usrflags & SYM_SCAN_LUNS_DISABLED) {
  807. if (sdev->lun != 0)
  808. return -ENXIO;
  809. starget_printk(KERN_INFO, tp->starget,
  810. "Multiple LUNs disabled in NVRAM\n");
  811. }
  812. lp = sym_alloc_lcb(np, sdev->id, sdev->lun);
  813. if (!lp)
  814. return -ENOMEM;
  815. spi_min_period(tp->starget) = tp->usr_period;
  816. spi_max_width(tp->starget) = tp->usr_width;
  817. return 0;
  818. }
  819. /*
  820. * Linux entry point for device queue sizing.
  821. */
  822. static int sym53c8xx_slave_configure(struct scsi_device *sdev)
  823. {
  824. struct sym_hcb *np = sym_get_hcb(sdev->host);
  825. struct sym_tcb *tp = &np->target[sdev->id];
  826. struct sym_lcb *lp = sym_lp(tp, sdev->lun);
  827. int reqtags, depth_to_use;
  828. /*
  829. * Get user flags.
  830. */
  831. lp->curr_flags = lp->user_flags;
  832. /*
  833. * Select queue depth from driver setup.
  834. * Donnot use more than configured by user.
  835. * Use at least 2.
  836. * Donnot use more than our maximum.
  837. */
  838. reqtags = device_queue_depth(np, sdev->id, sdev->lun);
  839. if (reqtags > tp->usrtags)
  840. reqtags = tp->usrtags;
  841. if (!sdev->tagged_supported)
  842. reqtags = 0;
  843. #if 1 /* Avoid to locally queue commands for no good reasons */
  844. if (reqtags > SYM_CONF_MAX_TAG)
  845. reqtags = SYM_CONF_MAX_TAG;
  846. depth_to_use = (reqtags ? reqtags : 2);
  847. #else
  848. depth_to_use = (reqtags ? SYM_CONF_MAX_TAG : 2);
  849. #endif
  850. scsi_adjust_queue_depth(sdev,
  851. (sdev->tagged_supported ?
  852. MSG_SIMPLE_TAG : 0),
  853. depth_to_use);
  854. lp->s.scdev_depth = depth_to_use;
  855. sym_tune_dev_queuing(tp, sdev->lun, reqtags);
  856. if (!spi_initial_dv(sdev->sdev_target))
  857. spi_dv_device(sdev);
  858. return 0;
  859. }
  860. static void sym53c8xx_slave_destroy(struct scsi_device *sdev)
  861. {
  862. struct sym_hcb *np = sym_get_hcb(sdev->host);
  863. struct sym_lcb *lp = sym_lp(&np->target[sdev->id], sdev->lun);
  864. if (lp->itlq_tbl)
  865. sym_mfree_dma(lp->itlq_tbl, SYM_CONF_MAX_TASK * 4, "ITLQ_TBL");
  866. kfree(lp->cb_tags);
  867. sym_mfree_dma(lp, sizeof(*lp), "LCB");
  868. }
  869. /*
  870. * Linux entry point for info() function
  871. */
  872. static const char *sym53c8xx_info (struct Scsi_Host *host)
  873. {
  874. return SYM_DRIVER_NAME;
  875. }
  876. #ifdef SYM_LINUX_PROC_INFO_SUPPORT
  877. /*
  878. * Proc file system stuff
  879. *
  880. * A read operation returns adapter information.
  881. * A write operation is a control command.
  882. * The string is parsed in the driver code and the command is passed
  883. * to the sym_usercmd() function.
  884. */
  885. #ifdef SYM_LINUX_USER_COMMAND_SUPPORT
  886. struct sym_usrcmd {
  887. u_long target;
  888. u_long lun;
  889. u_long data;
  890. u_long cmd;
  891. };
  892. #define UC_SETSYNC 10
  893. #define UC_SETTAGS 11
  894. #define UC_SETDEBUG 12
  895. #define UC_SETWIDE 14
  896. #define UC_SETFLAG 15
  897. #define UC_SETVERBOSE 17
  898. #define UC_RESETDEV 18
  899. #define UC_CLEARDEV 19
  900. static void sym_exec_user_command (struct sym_hcb *np, struct sym_usrcmd *uc)
  901. {
  902. struct sym_tcb *tp;
  903. int t, l;
  904. switch (uc->cmd) {
  905. case 0: return;
  906. #ifdef SYM_LINUX_DEBUG_CONTROL_SUPPORT
  907. case UC_SETDEBUG:
  908. sym_debug_flags = uc->data;
  909. break;
  910. #endif
  911. case UC_SETVERBOSE:
  912. np->verbose = uc->data;
  913. break;
  914. default:
  915. /*
  916. * We assume that other commands apply to targets.
  917. * This should always be the case and avoid the below
  918. * 4 lines to be repeated 6 times.
  919. */
  920. for (t = 0; t < SYM_CONF_MAX_TARGET; t++) {
  921. if (!((uc->target >> t) & 1))
  922. continue;
  923. tp = &np->target[t];
  924. switch (uc->cmd) {
  925. case UC_SETSYNC:
  926. if (!uc->data || uc->data >= 255) {
  927. tp->tgoal.iu = tp->tgoal.dt =
  928. tp->tgoal.qas = 0;
  929. tp->tgoal.offset = 0;
  930. } else if (uc->data <= 9 && np->minsync_dt) {
  931. if (uc->data < np->minsync_dt)
  932. uc->data = np->minsync_dt;
  933. tp->tgoal.iu = tp->tgoal.dt =
  934. tp->tgoal.qas = 1;
  935. tp->tgoal.width = 1;
  936. tp->tgoal.period = uc->data;
  937. tp->tgoal.offset = np->maxoffs_dt;
  938. } else {
  939. if (uc->data < np->minsync)
  940. uc->data = np->minsync;
  941. tp->tgoal.iu = tp->tgoal.dt =
  942. tp->tgoal.qas = 0;
  943. tp->tgoal.period = uc->data;
  944. tp->tgoal.offset = np->maxoffs;
  945. }
  946. tp->tgoal.check_nego = 1;
  947. break;
  948. case UC_SETWIDE:
  949. tp->tgoal.width = uc->data ? 1 : 0;
  950. tp->tgoal.check_nego = 1;
  951. break;
  952. case UC_SETTAGS:
  953. for (l = 0; l < SYM_CONF_MAX_LUN; l++)
  954. sym_tune_dev_queuing(tp, l, uc->data);
  955. break;
  956. case UC_RESETDEV:
  957. tp->to_reset = 1;
  958. np->istat_sem = SEM;
  959. OUTB(np, nc_istat, SIGP|SEM);
  960. break;
  961. case UC_CLEARDEV:
  962. for (l = 0; l < SYM_CONF_MAX_LUN; l++) {
  963. struct sym_lcb *lp = sym_lp(tp, l);
  964. if (lp) lp->to_clear = 1;
  965. }
  966. np->istat_sem = SEM;
  967. OUTB(np, nc_istat, SIGP|SEM);
  968. break;
  969. case UC_SETFLAG:
  970. tp->usrflags = uc->data;
  971. break;
  972. }
  973. }
  974. break;
  975. }
  976. }
  977. static int skip_spaces(char *ptr, int len)
  978. {
  979. int cnt, c;
  980. for (cnt = len; cnt > 0 && (c = *ptr++) && isspace(c); cnt--);
  981. return (len - cnt);
  982. }
  983. static int get_int_arg(char *ptr, int len, u_long *pv)
  984. {
  985. char *end;
  986. *pv = simple_strtoul(ptr, &end, 10);
  987. return (end - ptr);
  988. }
  989. static int is_keyword(char *ptr, int len, char *verb)
  990. {
  991. int verb_len = strlen(verb);
  992. if (len >= verb_len && !memcmp(verb, ptr, verb_len))
  993. return verb_len;
  994. else
  995. return 0;
  996. }
  997. #define SKIP_SPACES(ptr, len) \
  998. if ((arg_len = skip_spaces(ptr, len)) < 1) \
  999. return -EINVAL; \
  1000. ptr += arg_len; len -= arg_len;
  1001. #define GET_INT_ARG(ptr, len, v) \
  1002. if (!(arg_len = get_int_arg(ptr, len, &(v)))) \
  1003. return -EINVAL; \
  1004. ptr += arg_len; len -= arg_len;
  1005. /*
  1006. * Parse a control command
  1007. */
  1008. static int sym_user_command(struct sym_hcb *np, char *buffer, int length)
  1009. {
  1010. char *ptr = buffer;
  1011. int len = length;
  1012. struct sym_usrcmd cmd, *uc = &cmd;
  1013. int arg_len;
  1014. u_long target;
  1015. memset(uc, 0, sizeof(*uc));
  1016. if (len > 0 && ptr[len-1] == '\n')
  1017. --len;
  1018. if ((arg_len = is_keyword(ptr, len, "setsync")) != 0)
  1019. uc->cmd = UC_SETSYNC;
  1020. else if ((arg_len = is_keyword(ptr, len, "settags")) != 0)
  1021. uc->cmd = UC_SETTAGS;
  1022. else if ((arg_len = is_keyword(ptr, len, "setverbose")) != 0)
  1023. uc->cmd = UC_SETVERBOSE;
  1024. else if ((arg_len = is_keyword(ptr, len, "setwide")) != 0)
  1025. uc->cmd = UC_SETWIDE;
  1026. #ifdef SYM_LINUX_DEBUG_CONTROL_SUPPORT
  1027. else if ((arg_len = is_keyword(ptr, len, "setdebug")) != 0)
  1028. uc->cmd = UC_SETDEBUG;
  1029. #endif
  1030. else if ((arg_len = is_keyword(ptr, len, "setflag")) != 0)
  1031. uc->cmd = UC_SETFLAG;
  1032. else if ((arg_len = is_keyword(ptr, len, "resetdev")) != 0)
  1033. uc->cmd = UC_RESETDEV;
  1034. else if ((arg_len = is_keyword(ptr, len, "cleardev")) != 0)
  1035. uc->cmd = UC_CLEARDEV;
  1036. else
  1037. arg_len = 0;
  1038. #ifdef DEBUG_PROC_INFO
  1039. printk("sym_user_command: arg_len=%d, cmd=%ld\n", arg_len, uc->cmd);
  1040. #endif
  1041. if (!arg_len)
  1042. return -EINVAL;
  1043. ptr += arg_len; len -= arg_len;
  1044. switch(uc->cmd) {
  1045. case UC_SETSYNC:
  1046. case UC_SETTAGS:
  1047. case UC_SETWIDE:
  1048. case UC_SETFLAG:
  1049. case UC_RESETDEV:
  1050. case UC_CLEARDEV:
  1051. SKIP_SPACES(ptr, len);
  1052. if ((arg_len = is_keyword(ptr, len, "all")) != 0) {
  1053. ptr += arg_len; len -= arg_len;
  1054. uc->target = ~0;
  1055. } else {
  1056. GET_INT_ARG(ptr, len, target);
  1057. uc->target = (1<<target);
  1058. #ifdef DEBUG_PROC_INFO
  1059. printk("sym_user_command: target=%ld\n", target);
  1060. #endif
  1061. }
  1062. break;
  1063. }
  1064. switch(uc->cmd) {
  1065. case UC_SETVERBOSE:
  1066. case UC_SETSYNC:
  1067. case UC_SETTAGS:
  1068. case UC_SETWIDE:
  1069. SKIP_SPACES(ptr, len);
  1070. GET_INT_ARG(ptr, len, uc->data);
  1071. #ifdef DEBUG_PROC_INFO
  1072. printk("sym_user_command: data=%ld\n", uc->data);
  1073. #endif
  1074. break;
  1075. #ifdef SYM_LINUX_DEBUG_CONTROL_SUPPORT
  1076. case UC_SETDEBUG:
  1077. while (len > 0) {
  1078. SKIP_SPACES(ptr, len);
  1079. if ((arg_len = is_keyword(ptr, len, "alloc")))
  1080. uc->data |= DEBUG_ALLOC;
  1081. else if ((arg_len = is_keyword(ptr, len, "phase")))
  1082. uc->data |= DEBUG_PHASE;
  1083. else if ((arg_len = is_keyword(ptr, len, "queue")))
  1084. uc->data |= DEBUG_QUEUE;
  1085. else if ((arg_len = is_keyword(ptr, len, "result")))
  1086. uc->data |= DEBUG_RESULT;
  1087. else if ((arg_len = is_keyword(ptr, len, "scatter")))
  1088. uc->data |= DEBUG_SCATTER;
  1089. else if ((arg_len = is_keyword(ptr, len, "script")))
  1090. uc->data |= DEBUG_SCRIPT;
  1091. else if ((arg_len = is_keyword(ptr, len, "tiny")))
  1092. uc->data |= DEBUG_TINY;
  1093. else if ((arg_len = is_keyword(ptr, len, "timing")))
  1094. uc->data |= DEBUG_TIMING;
  1095. else if ((arg_len = is_keyword(ptr, len, "nego")))
  1096. uc->data |= DEBUG_NEGO;
  1097. else if ((arg_len = is_keyword(ptr, len, "tags")))
  1098. uc->data |= DEBUG_TAGS;
  1099. else if ((arg_len = is_keyword(ptr, len, "pointer")))
  1100. uc->data |= DEBUG_POINTER;
  1101. else
  1102. return -EINVAL;
  1103. ptr += arg_len; len -= arg_len;
  1104. }
  1105. #ifdef DEBUG_PROC_INFO
  1106. printk("sym_user_command: data=%ld\n", uc->data);
  1107. #endif
  1108. break;
  1109. #endif /* SYM_LINUX_DEBUG_CONTROL_SUPPORT */
  1110. case UC_SETFLAG:
  1111. while (len > 0) {
  1112. SKIP_SPACES(ptr, len);
  1113. if ((arg_len = is_keyword(ptr, len, "no_disc")))
  1114. uc->data &= ~SYM_DISC_ENABLED;
  1115. else
  1116. return -EINVAL;
  1117. ptr += arg_len; len -= arg_len;
  1118. }
  1119. break;
  1120. default:
  1121. break;
  1122. }
  1123. if (len)
  1124. return -EINVAL;
  1125. else {
  1126. unsigned long flags;
  1127. spin_lock_irqsave(np->s.host->host_lock, flags);
  1128. sym_exec_user_command (np, uc);
  1129. spin_unlock_irqrestore(np->s.host->host_lock, flags);
  1130. }
  1131. return length;
  1132. }
  1133. #endif /* SYM_LINUX_USER_COMMAND_SUPPORT */
  1134. #ifdef SYM_LINUX_USER_INFO_SUPPORT
  1135. /*
  1136. * Informations through the proc file system.
  1137. */
  1138. struct info_str {
  1139. char *buffer;
  1140. int length;
  1141. int offset;
  1142. int pos;
  1143. };
  1144. static void copy_mem_info(struct info_str *info, char *data, int len)
  1145. {
  1146. if (info->pos + len > info->length)
  1147. len = info->length - info->pos;
  1148. if (info->pos + len < info->offset) {
  1149. info->pos += len;
  1150. return;
  1151. }
  1152. if (info->pos < info->offset) {
  1153. data += (info->offset - info->pos);
  1154. len -= (info->offset - info->pos);
  1155. }
  1156. if (len > 0) {
  1157. memcpy(info->buffer + info->pos, data, len);
  1158. info->pos += len;
  1159. }
  1160. }
  1161. static int copy_info(struct info_str *info, char *fmt, ...)
  1162. {
  1163. va_list args;
  1164. char buf[81];
  1165. int len;
  1166. va_start(args, fmt);
  1167. len = vsprintf(buf, fmt, args);
  1168. va_end(args);
  1169. copy_mem_info(info, buf, len);
  1170. return len;
  1171. }
  1172. /*
  1173. * Copy formatted information into the input buffer.
  1174. */
  1175. static int sym_host_info(struct sym_hcb *np, char *ptr, off_t offset, int len)
  1176. {
  1177. struct info_str info;
  1178. info.buffer = ptr;
  1179. info.length = len;
  1180. info.offset = offset;
  1181. info.pos = 0;
  1182. copy_info(&info, "Chip " NAME53C "%s, device id 0x%x, "
  1183. "revision id 0x%x\n",
  1184. np->s.chip_name, np->device_id, np->revision_id);
  1185. copy_info(&info, "At PCI address %s, IRQ " IRQ_FMT "\n",
  1186. pci_name(np->s.device), IRQ_PRM(np->s.irq));
  1187. copy_info(&info, "Min. period factor %d, %s SCSI BUS%s\n",
  1188. (int) (np->minsync_dt ? np->minsync_dt : np->minsync),
  1189. np->maxwide ? "Wide" : "Narrow",
  1190. np->minsync_dt ? ", DT capable" : "");
  1191. copy_info(&info, "Max. started commands %d, "
  1192. "max. commands per LUN %d\n",
  1193. SYM_CONF_MAX_START, SYM_CONF_MAX_TAG);
  1194. return info.pos > info.offset? info.pos - info.offset : 0;
  1195. }
  1196. #endif /* SYM_LINUX_USER_INFO_SUPPORT */
  1197. /*
  1198. * Entry point of the scsi proc fs of the driver.
  1199. * - func = 0 means read (returns adapter infos)
  1200. * - func = 1 means write (not yet merget from sym53c8xx)
  1201. */
  1202. static int sym53c8xx_proc_info(struct Scsi_Host *host, char *buffer,
  1203. char **start, off_t offset, int length, int func)
  1204. {
  1205. struct sym_hcb *np = sym_get_hcb(host);
  1206. int retv;
  1207. if (func) {
  1208. #ifdef SYM_LINUX_USER_COMMAND_SUPPORT
  1209. retv = sym_user_command(np, buffer, length);
  1210. #else
  1211. retv = -EINVAL;
  1212. #endif
  1213. } else {
  1214. if (start)
  1215. *start = buffer;
  1216. #ifdef SYM_LINUX_USER_INFO_SUPPORT
  1217. retv = sym_host_info(np, buffer, offset, length);
  1218. #else
  1219. retv = -EINVAL;
  1220. #endif
  1221. }
  1222. return retv;
  1223. }
  1224. #endif /* SYM_LINUX_PROC_INFO_SUPPORT */
  1225. /*
  1226. * Free controller resources.
  1227. */
  1228. static void sym_free_resources(struct sym_hcb *np, struct pci_dev *pdev)
  1229. {
  1230. /*
  1231. * Free O/S specific resources.
  1232. */
  1233. if (np->s.irq)
  1234. free_irq(np->s.irq, np);
  1235. if (np->s.ioaddr)
  1236. pci_iounmap(pdev, np->s.ioaddr);
  1237. if (np->s.ramaddr)
  1238. pci_iounmap(pdev, np->s.ramaddr);
  1239. /*
  1240. * Free O/S independent resources.
  1241. */
  1242. sym_hcb_free(np);
  1243. sym_mfree_dma(np, sizeof(*np), "HCB");
  1244. }
  1245. /*
  1246. * Ask/tell the system about DMA addressing.
  1247. */
  1248. static int sym_setup_bus_dma_mask(struct sym_hcb *np)
  1249. {
  1250. #if SYM_CONF_DMA_ADDRESSING_MODE > 0
  1251. #if SYM_CONF_DMA_ADDRESSING_MODE == 1
  1252. #define DMA_DAC_MASK DMA_40BIT_MASK
  1253. #elif SYM_CONF_DMA_ADDRESSING_MODE == 2
  1254. #define DMA_DAC_MASK DMA_64BIT_MASK
  1255. #endif
  1256. if ((np->features & FE_DAC) &&
  1257. !pci_set_dma_mask(np->s.device, DMA_DAC_MASK)) {
  1258. np->use_dac = 1;
  1259. return 0;
  1260. }
  1261. #endif
  1262. if (!pci_set_dma_mask(np->s.device, DMA_32BIT_MASK))
  1263. return 0;
  1264. printf_warning("%s: No suitable DMA available\n", sym_name(np));
  1265. return -1;
  1266. }
  1267. /*
  1268. * Host attach and initialisations.
  1269. *
  1270. * Allocate host data and ncb structure.
  1271. * Remap MMIO region.
  1272. * Do chip initialization.
  1273. * If all is OK, install interrupt handling and
  1274. * start the timer daemon.
  1275. */
  1276. static struct Scsi_Host * __devinit sym_attach(struct scsi_host_template *tpnt,
  1277. int unit, struct sym_device *dev)
  1278. {
  1279. struct host_data *host_data;
  1280. struct sym_hcb *np = NULL;
  1281. struct Scsi_Host *instance = NULL;
  1282. struct pci_dev *pdev = dev->pdev;
  1283. unsigned long flags;
  1284. struct sym_fw *fw;
  1285. printk(KERN_INFO
  1286. "sym%d: <%s> rev 0x%x at pci %s irq " IRQ_FMT "\n",
  1287. unit, dev->chip.name, dev->chip.revision_id,
  1288. pci_name(pdev), IRQ_PRM(pdev->irq));
  1289. /*
  1290. * Get the firmware for this chip.
  1291. */
  1292. fw = sym_find_firmware(&dev->chip);
  1293. if (!fw)
  1294. goto attach_failed;
  1295. /*
  1296. * Allocate host_data structure
  1297. */
  1298. instance = scsi_host_alloc(tpnt, sizeof(*host_data));
  1299. if (!instance)
  1300. goto attach_failed;
  1301. host_data = (struct host_data *) instance->hostdata;
  1302. /*
  1303. * Allocate immediately the host control block,
  1304. * since we are only expecting to succeed. :)
  1305. * We keep track in the HCB of all the resources that
  1306. * are to be released on error.
  1307. */
  1308. np = __sym_calloc_dma(&pdev->dev, sizeof(*np), "HCB");
  1309. if (!np)
  1310. goto attach_failed;
  1311. np->s.device = pdev;
  1312. np->bus_dmat = &pdev->dev; /* Result in 1 DMA pool per HBA */
  1313. host_data->ncb = np;
  1314. np->s.host = instance;
  1315. pci_set_drvdata(pdev, np);
  1316. /*
  1317. * Copy some useful infos to the HCB.
  1318. */
  1319. np->hcb_ba = vtobus(np);
  1320. np->verbose = sym_driver_setup.verbose;
  1321. np->s.device = pdev;
  1322. np->s.unit = unit;
  1323. np->device_id = dev->chip.device_id;
  1324. np->revision_id = dev->chip.revision_id;
  1325. np->features = dev->chip.features;
  1326. np->clock_divn = dev->chip.nr_divisor;
  1327. np->maxoffs = dev->chip.offset_max;
  1328. np->maxburst = dev->chip.burst_max;
  1329. np->myaddr = dev->host_id;
  1330. /*
  1331. * Edit its name.
  1332. */
  1333. strlcpy(np->s.chip_name, dev->chip.name, sizeof(np->s.chip_name));
  1334. sprintf(np->s.inst_name, "sym%d", np->s.unit);
  1335. if (sym_setup_bus_dma_mask(np))
  1336. goto attach_failed;
  1337. /*
  1338. * Try to map the controller chip to
  1339. * virtual and physical memory.
  1340. */
  1341. np->mmio_ba = (u32)dev->mmio_base;
  1342. np->s.ioaddr = dev->s.ioaddr;
  1343. np->s.ramaddr = dev->s.ramaddr;
  1344. np->s.io_ws = (np->features & FE_IO256) ? 256 : 128;
  1345. /*
  1346. * Map on-chip RAM if present and supported.
  1347. */
  1348. if (!(np->features & FE_RAM))
  1349. dev->ram_base = 0;
  1350. if (dev->ram_base) {
  1351. np->ram_ba = (u32)dev->ram_base;
  1352. np->ram_ws = (np->features & FE_RAM8K) ? 8192 : 4096;
  1353. }
  1354. if (sym_hcb_attach(instance, fw, dev->nvram))
  1355. goto attach_failed;
  1356. /*
  1357. * Install the interrupt handler.
  1358. * If we synchonize the C code with SCRIPTS on interrupt,
  1359. * we do not want to share the INTR line at all.
  1360. */
  1361. if (request_irq(pdev->irq, sym53c8xx_intr, SA_SHIRQ, NAME53C8XX, np)) {
  1362. printf_err("%s: request irq %d failure\n",
  1363. sym_name(np), pdev->irq);
  1364. goto attach_failed;
  1365. }
  1366. np->s.irq = pdev->irq;
  1367. /*
  1368. * After SCSI devices have been opened, we cannot
  1369. * reset the bus safely, so we do it here.
  1370. */
  1371. spin_lock_irqsave(instance->host_lock, flags);
  1372. if (sym_reset_scsi_bus(np, 0))
  1373. goto reset_failed;
  1374. /*
  1375. * Start the SCRIPTS.
  1376. */
  1377. sym_start_up (np, 1);
  1378. /*
  1379. * Start the timer daemon
  1380. */
  1381. init_timer(&np->s.timer);
  1382. np->s.timer.data = (unsigned long) np;
  1383. np->s.timer.function = sym53c8xx_timer;
  1384. np->s.lasttime=0;
  1385. sym_timer (np);
  1386. /*
  1387. * Fill Linux host instance structure
  1388. * and return success.
  1389. */
  1390. instance->max_channel = 0;
  1391. instance->this_id = np->myaddr;
  1392. instance->max_id = np->maxwide ? 16 : 8;
  1393. instance->max_lun = SYM_CONF_MAX_LUN;
  1394. instance->unique_id = pci_resource_start(pdev, 0);
  1395. instance->cmd_per_lun = SYM_CONF_MAX_TAG;
  1396. instance->can_queue = (SYM_CONF_MAX_START-2);
  1397. instance->sg_tablesize = SYM_CONF_MAX_SG;
  1398. instance->max_cmd_len = 16;
  1399. BUG_ON(sym2_transport_template == NULL);
  1400. instance->transportt = sym2_transport_template;
  1401. spin_unlock_irqrestore(instance->host_lock, flags);
  1402. return instance;
  1403. reset_failed:
  1404. printf_err("%s: FATAL ERROR: CHECK SCSI BUS - CABLES, "
  1405. "TERMINATION, DEVICE POWER etc.!\n", sym_name(np));
  1406. spin_unlock_irqrestore(instance->host_lock, flags);
  1407. attach_failed:
  1408. if (!instance)
  1409. return NULL;
  1410. printf_info("%s: giving up ...\n", sym_name(np));
  1411. if (np)
  1412. sym_free_resources(np, pdev);
  1413. scsi_host_put(instance);
  1414. return NULL;
  1415. }
  1416. /*
  1417. * Detect and try to read SYMBIOS and TEKRAM NVRAM.
  1418. */
  1419. #if SYM_CONF_NVRAM_SUPPORT
  1420. static void __devinit sym_get_nvram(struct sym_device *devp, struct sym_nvram *nvp)
  1421. {
  1422. devp->nvram = nvp;
  1423. devp->device_id = devp->chip.device_id;
  1424. nvp->type = 0;
  1425. sym_read_nvram(devp, nvp);
  1426. }
  1427. #else
  1428. static inline void sym_get_nvram(struct sym_device *devp, struct sym_nvram *nvp)
  1429. {
  1430. }
  1431. #endif /* SYM_CONF_NVRAM_SUPPORT */
  1432. static int __devinit sym_check_supported(struct sym_device *device)
  1433. {
  1434. struct sym_chip *chip;
  1435. struct pci_dev *pdev = device->pdev;
  1436. u_char revision;
  1437. unsigned long io_port = pci_resource_start(pdev, 0);
  1438. int i;
  1439. /*
  1440. * If user excluded this chip, do not initialize it.
  1441. * I hate this code so much. Must kill it.
  1442. */
  1443. if (io_port) {
  1444. for (i = 0 ; i < 8 ; i++) {
  1445. if (sym_driver_setup.excludes[i] == io_port)
  1446. return -ENODEV;
  1447. }
  1448. }
  1449. /*
  1450. * Check if the chip is supported. Then copy the chip description
  1451. * to our device structure so we can make it match the actual device
  1452. * and options.
  1453. */
  1454. pci_read_config_byte(pdev, PCI_CLASS_REVISION, &revision);
  1455. chip = sym_lookup_chip_table(pdev->device, revision);
  1456. if (!chip) {
  1457. dev_info(&pdev->dev, "device not supported\n");
  1458. return -ENODEV;
  1459. }
  1460. memcpy(&device->chip, chip, sizeof(device->chip));
  1461. device->chip.revision_id = revision;
  1462. return 0;
  1463. }
  1464. /*
  1465. * Ignore Symbios chips controlled by various RAID controllers.
  1466. * These controllers set value 0x52414944 at RAM end - 16.
  1467. */
  1468. static int __devinit sym_check_raid(struct sym_device *device)
  1469. {
  1470. unsigned int ram_size, ram_val;
  1471. if (!device->s.ramaddr)
  1472. return 0;
  1473. if (device->chip.features & FE_RAM8K)
  1474. ram_size = 8192;
  1475. else
  1476. ram_size = 4096;
  1477. ram_val = readl(device->s.ramaddr + ram_size - 16);
  1478. if (ram_val != 0x52414944)
  1479. return 0;
  1480. dev_info(&device->pdev->dev,
  1481. "not initializing, driven by RAID controller.\n");
  1482. return -ENODEV;
  1483. }
  1484. static int __devinit sym_set_workarounds(struct sym_device *device)
  1485. {
  1486. struct sym_chip *chip = &device->chip;
  1487. struct pci_dev *pdev = device->pdev;
  1488. u_short status_reg;
  1489. /*
  1490. * (ITEM 12 of a DEL about the 896 I haven't yet).
  1491. * We must ensure the chip will use WRITE AND INVALIDATE.
  1492. * The revision number limit is for now arbitrary.
  1493. */
  1494. if (pdev->device == PCI_DEVICE_ID_NCR_53C896 && chip->revision_id < 0x4) {
  1495. chip->features |= (FE_WRIE | FE_CLSE);
  1496. }
  1497. /* If the chip can do Memory Write Invalidate, enable it */
  1498. if (chip->features & FE_WRIE) {
  1499. if (pci_set_mwi(pdev))
  1500. return -ENODEV;
  1501. }
  1502. /*
  1503. * Work around for errant bit in 895A. The 66Mhz
  1504. * capable bit is set erroneously. Clear this bit.
  1505. * (Item 1 DEL 533)
  1506. *
  1507. * Make sure Config space and Features agree.
  1508. *
  1509. * Recall: writes are not normal to status register -
  1510. * write a 1 to clear and a 0 to leave unchanged.
  1511. * Can only reset bits.
  1512. */
  1513. pci_read_config_word(pdev, PCI_STATUS, &status_reg);
  1514. if (chip->features & FE_66MHZ) {
  1515. if (!(status_reg & PCI_STATUS_66MHZ))
  1516. chip->features &= ~FE_66MHZ;
  1517. } else {
  1518. if (status_reg & PCI_STATUS_66MHZ) {
  1519. status_reg = PCI_STATUS_66MHZ;
  1520. pci_write_config_word(pdev, PCI_STATUS, status_reg);
  1521. pci_read_config_word(pdev, PCI_STATUS, &status_reg);
  1522. }
  1523. }
  1524. return 0;
  1525. }
  1526. /*
  1527. * Read and check the PCI configuration for any detected NCR
  1528. * boards and save data for attaching after all boards have
  1529. * been detected.
  1530. */
  1531. static void __devinit
  1532. sym_init_device(struct pci_dev *pdev, struct sym_device *device)
  1533. {
  1534. int i = 2;
  1535. struct pci_bus_region bus_addr;
  1536. device->host_id = SYM_SETUP_HOST_ID;
  1537. device->pdev = pdev;
  1538. pcibios_resource_to_bus(pdev, &bus_addr, &pdev->resource[1]);
  1539. device->mmio_base = bus_addr.start;
  1540. /*
  1541. * If the BAR is 64-bit, resource 2 will be occupied by the
  1542. * upper 32 bits
  1543. */
  1544. if (!pdev->resource[i].flags)
  1545. i++;
  1546. pcibios_resource_to_bus(pdev, &bus_addr, &pdev->resource[i]);
  1547. device->ram_base = bus_addr.start;
  1548. #ifdef CONFIG_SCSI_SYM53C8XX_MMIO
  1549. if (device->mmio_base)
  1550. device->s.ioaddr = pci_iomap(pdev, 1,
  1551. pci_resource_len(pdev, 1));
  1552. #endif
  1553. if (!device->s.ioaddr)
  1554. device->s.ioaddr = pci_iomap(pdev, 0,
  1555. pci_resource_len(pdev, 0));
  1556. if (device->ram_base)
  1557. device->s.ramaddr = pci_iomap(pdev, i,
  1558. pci_resource_len(pdev, i));
  1559. }
  1560. /*
  1561. * The NCR PQS and PDS cards are constructed as a DEC bridge
  1562. * behind which sits a proprietary NCR memory controller and
  1563. * either four or two 53c875s as separate devices. We can tell
  1564. * if an 875 is part of a PQS/PDS or not since if it is, it will
  1565. * be on the same bus as the memory controller. In its usual
  1566. * mode of operation, the 875s are slaved to the memory
  1567. * controller for all transfers. To operate with the Linux
  1568. * driver, the memory controller is disabled and the 875s
  1569. * freed to function independently. The only wrinkle is that
  1570. * the preset SCSI ID (which may be zero) must be read in from
  1571. * a special configuration space register of the 875.
  1572. */
  1573. static void sym_config_pqs(struct pci_dev *pdev, struct sym_device *sym_dev)
  1574. {
  1575. int slot;
  1576. u8 tmp;
  1577. for (slot = 0; slot < 256; slot++) {
  1578. struct pci_dev *memc = pci_get_slot(pdev->bus, slot);
  1579. if (!memc || memc->vendor != 0x101a || memc->device == 0x0009) {
  1580. pci_dev_put(memc);
  1581. continue;
  1582. }
  1583. /* bit 1: allow individual 875 configuration */
  1584. pci_read_config_byte(memc, 0x44, &tmp);
  1585. if ((tmp & 0x2) == 0) {
  1586. tmp |= 0x2;
  1587. pci_write_config_byte(memc, 0x44, tmp);
  1588. }
  1589. /* bit 2: drive individual 875 interrupts to the bus */
  1590. pci_read_config_byte(memc, 0x45, &tmp);
  1591. if ((tmp & 0x4) == 0) {
  1592. tmp |= 0x4;
  1593. pci_write_config_byte(memc, 0x45, tmp);
  1594. }
  1595. pci_dev_put(memc);
  1596. break;
  1597. }
  1598. pci_read_config_byte(pdev, 0x84, &tmp);
  1599. sym_dev->host_id = tmp;
  1600. }
  1601. /*
  1602. * Called before unloading the module.
  1603. * Detach the host.
  1604. * We have to free resources and halt the NCR chip.
  1605. */
  1606. static int sym_detach(struct sym_hcb *np, struct pci_dev *pdev)
  1607. {
  1608. printk("%s: detaching ...\n", sym_name(np));
  1609. del_timer_sync(&np->s.timer);
  1610. /*
  1611. * Reset NCR chip.
  1612. * We should use sym_soft_reset(), but we don't want to do
  1613. * so, since we may not be safe if interrupts occur.
  1614. */
  1615. printk("%s: resetting chip\n", sym_name(np));
  1616. OUTB(np, nc_istat, SRST);
  1617. INB(np, nc_mbox1);
  1618. udelay(10);
  1619. OUTB(np, nc_istat, 0);
  1620. sym_free_resources(np, pdev);
  1621. return 1;
  1622. }
  1623. /*
  1624. * Driver host template.
  1625. */
  1626. static struct scsi_host_template sym2_template = {
  1627. .module = THIS_MODULE,
  1628. .name = "sym53c8xx",
  1629. .info = sym53c8xx_info,
  1630. .queuecommand = sym53c8xx_queue_command,
  1631. .slave_alloc = sym53c8xx_slave_alloc,
  1632. .slave_configure = sym53c8xx_slave_configure,
  1633. .slave_destroy = sym53c8xx_slave_destroy,
  1634. .eh_abort_handler = sym53c8xx_eh_abort_handler,
  1635. .eh_device_reset_handler = sym53c8xx_eh_device_reset_handler,
  1636. .eh_bus_reset_handler = sym53c8xx_eh_bus_reset_handler,
  1637. .eh_host_reset_handler = sym53c8xx_eh_host_reset_handler,
  1638. .this_id = 7,
  1639. .use_clustering = ENABLE_CLUSTERING,
  1640. .max_sectors = 0xFFFF,
  1641. #ifdef SYM_LINUX_PROC_INFO_SUPPORT
  1642. .proc_info = sym53c8xx_proc_info,
  1643. .proc_name = NAME53C8XX,
  1644. #endif
  1645. };
  1646. static int attach_count;
  1647. static int __devinit sym2_probe(struct pci_dev *pdev,
  1648. const struct pci_device_id *ent)
  1649. {
  1650. struct sym_device sym_dev;
  1651. struct sym_nvram nvram;
  1652. struct Scsi_Host *instance;
  1653. memset(&sym_dev, 0, sizeof(sym_dev));
  1654. memset(&nvram, 0, sizeof(nvram));
  1655. if (pci_enable_device(pdev))
  1656. goto leave;
  1657. pci_set_master(pdev);
  1658. if (pci_request_regions(pdev, NAME53C8XX))
  1659. goto disable;
  1660. sym_init_device(pdev, &sym_dev);
  1661. if (sym_check_supported(&sym_dev))
  1662. goto free;
  1663. if (sym_check_raid(&sym_dev))
  1664. goto leave; /* Don't disable the device */
  1665. if (sym_set_workarounds(&sym_dev))
  1666. goto free;
  1667. sym_config_pqs(pdev, &sym_dev);
  1668. sym_get_nvram(&sym_dev, &nvram);
  1669. instance = sym_attach(&sym2_template, attach_count, &sym_dev);
  1670. if (!instance)
  1671. goto free;
  1672. if (scsi_add_host(instance, &pdev->dev))
  1673. goto detach;
  1674. scsi_scan_host(instance);
  1675. attach_count++;
  1676. return 0;
  1677. detach:
  1678. sym_detach(pci_get_drvdata(pdev), pdev);
  1679. free:
  1680. pci_release_regions(pdev);
  1681. disable:
  1682. pci_disable_device(pdev);
  1683. leave:
  1684. return -ENODEV;
  1685. }
  1686. static void __devexit sym2_remove(struct pci_dev *pdev)
  1687. {
  1688. struct sym_hcb *np = pci_get_drvdata(pdev);
  1689. struct Scsi_Host *host = np->s.host;
  1690. scsi_remove_host(host);
  1691. scsi_host_put(host);
  1692. sym_detach(np, pdev);
  1693. pci_release_regions(pdev);
  1694. pci_disable_device(pdev);
  1695. attach_count--;
  1696. }
  1697. static void sym2_get_signalling(struct Scsi_Host *shost)
  1698. {
  1699. struct sym_hcb *np = sym_get_hcb(shost);
  1700. enum spi_signal_type type;
  1701. switch (np->scsi_mode) {
  1702. case SMODE_SE:
  1703. type = SPI_SIGNAL_SE;
  1704. break;
  1705. case SMODE_LVD:
  1706. type = SPI_SIGNAL_LVD;
  1707. break;
  1708. case SMODE_HVD:
  1709. type = SPI_SIGNAL_HVD;
  1710. break;
  1711. default:
  1712. type = SPI_SIGNAL_UNKNOWN;
  1713. break;
  1714. }
  1715. spi_signalling(shost) = type;
  1716. }
  1717. static void sym2_set_offset(struct scsi_target *starget, int offset)
  1718. {
  1719. struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
  1720. struct sym_hcb *np = sym_get_hcb(shost);
  1721. struct sym_tcb *tp = &np->target[starget->id];
  1722. tp->tgoal.offset = offset;
  1723. tp->tgoal.check_nego = 1;
  1724. }
  1725. static void sym2_set_period(struct scsi_target *starget, int period)
  1726. {
  1727. struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
  1728. struct sym_hcb *np = sym_get_hcb(shost);
  1729. struct sym_tcb *tp = &np->target[starget->id];
  1730. /* have to have DT for these transfers, but DT will also
  1731. * set width, so check that this is allowed */
  1732. if (period <= np->minsync && spi_width(starget))
  1733. tp->tgoal.dt = 1;
  1734. tp->tgoal.period = period;
  1735. tp->tgoal.check_nego = 1;
  1736. }
  1737. static void sym2_set_width(struct scsi_target *starget, int width)
  1738. {
  1739. struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
  1740. struct sym_hcb *np = sym_get_hcb(shost);
  1741. struct sym_tcb *tp = &np->target[starget->id];
  1742. /* It is illegal to have DT set on narrow transfers. If DT is
  1743. * clear, we must also clear IU and QAS. */
  1744. if (width == 0)
  1745. tp->tgoal.iu = tp->tgoal.dt = tp->tgoal.qas = 0;
  1746. tp->tgoal.width = width;
  1747. tp->tgoal.check_nego = 1;
  1748. }
  1749. static void sym2_set_dt(struct scsi_target *starget, int dt)
  1750. {
  1751. struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
  1752. struct sym_hcb *np = sym_get_hcb(shost);
  1753. struct sym_tcb *tp = &np->target[starget->id];
  1754. /* We must clear QAS and IU if DT is clear */
  1755. if (dt)
  1756. tp->tgoal.dt = 1;
  1757. else
  1758. tp->tgoal.iu = tp->tgoal.dt = tp->tgoal.qas = 0;
  1759. tp->tgoal.check_nego = 1;
  1760. }
  1761. #if 0
  1762. static void sym2_set_iu(struct scsi_target *starget, int iu)
  1763. {
  1764. struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
  1765. struct sym_hcb *np = sym_get_hcb(shost);
  1766. struct sym_tcb *tp = &np->target[starget->id];
  1767. if (iu)
  1768. tp->tgoal.iu = tp->tgoal.dt = 1;
  1769. else
  1770. tp->tgoal.iu = 0;
  1771. tp->tgoal.check_nego = 1;
  1772. }
  1773. static void sym2_set_qas(struct scsi_target *starget, int qas)
  1774. {
  1775. struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
  1776. struct sym_hcb *np = sym_get_hcb(shost);
  1777. struct sym_tcb *tp = &np->target[starget->id];
  1778. if (qas)
  1779. tp->tgoal.dt = tp->tgoal.qas = 1;
  1780. else
  1781. tp->tgoal.qas = 0;
  1782. tp->tgoal.check_nego = 1;
  1783. }
  1784. #endif
  1785. static struct spi_function_template sym2_transport_functions = {
  1786. .set_offset = sym2_set_offset,
  1787. .show_offset = 1,
  1788. .set_period = sym2_set_period,
  1789. .show_period = 1,
  1790. .set_width = sym2_set_width,
  1791. .show_width = 1,
  1792. .set_dt = sym2_set_dt,
  1793. .show_dt = 1,
  1794. #if 0
  1795. .set_iu = sym2_set_iu,
  1796. .show_iu = 1,
  1797. .set_qas = sym2_set_qas,
  1798. .show_qas = 1,
  1799. #endif
  1800. .get_signalling = sym2_get_signalling,
  1801. };
  1802. static struct pci_device_id sym2_id_table[] __devinitdata = {
  1803. { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_NCR_53C810,
  1804. PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0UL },
  1805. { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_NCR_53C820,
  1806. PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0UL }, /* new */
  1807. { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_NCR_53C825,
  1808. PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0UL },
  1809. { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_NCR_53C815,
  1810. PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0UL },
  1811. { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_53C810AP,
  1812. PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0UL }, /* new */
  1813. { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_NCR_53C860,
  1814. PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0UL },
  1815. { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_53C1510,
  1816. PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0UL },
  1817. { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_NCR_53C896,
  1818. PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0UL },
  1819. { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_NCR_53C895,
  1820. PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0UL },
  1821. { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_NCR_53C885,
  1822. PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0UL },
  1823. { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_NCR_53C875,
  1824. PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0UL },
  1825. { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_NCR_53C1510,
  1826. PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0UL }, /* new */
  1827. { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_53C895A,
  1828. PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0UL },
  1829. { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_53C875A,
  1830. PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0UL },
  1831. { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_53C1010_33,
  1832. PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0UL },
  1833. { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_53C1010_66,
  1834. PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0UL },
  1835. { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_NCR_53C875J,
  1836. PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0UL },
  1837. { 0, }
  1838. };
  1839. MODULE_DEVICE_TABLE(pci, sym2_id_table);
  1840. static struct pci_driver sym2_driver = {
  1841. .name = NAME53C8XX,
  1842. .id_table = sym2_id_table,
  1843. .probe = sym2_probe,
  1844. .remove = __devexit_p(sym2_remove),
  1845. };
  1846. static int __init sym2_init(void)
  1847. {
  1848. int error;
  1849. sym2_setup_params();
  1850. sym2_transport_template = spi_attach_transport(&sym2_transport_functions);
  1851. if (!sym2_transport_template)
  1852. return -ENODEV;
  1853. error = pci_register_driver(&sym2_driver);
  1854. if (error)
  1855. spi_release_transport(sym2_transport_template);
  1856. return error;
  1857. }
  1858. static void __exit sym2_exit(void)
  1859. {
  1860. pci_unregister_driver(&sym2_driver);
  1861. spi_release_transport(sym2_transport_template);
  1862. }
  1863. module_init(sym2_init);
  1864. module_exit(sym2_exit);