aic94xx_hwi.c 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Aic94xx SAS/SATA driver hardware interface.
  4. *
  5. * Copyright (C) 2005 Adaptec, Inc. All rights reserved.
  6. * Copyright (C) 2005 Luben Tuikov <[email protected]>
  7. */
  8. #include <linux/pci.h>
  9. #include <linux/slab.h>
  10. #include <linux/delay.h>
  11. #include <linux/module.h>
  12. #include <linux/firmware.h>
  13. #include "aic94xx.h"
  14. #include "aic94xx_reg.h"
  15. #include "aic94xx_hwi.h"
  16. #include "aic94xx_seq.h"
  17. #include "aic94xx_dump.h"
  18. u32 MBAR0_SWB_SIZE;
  19. /* ---------- Initialization ---------- */
  20. static int asd_get_user_sas_addr(struct asd_ha_struct *asd_ha)
  21. {
  22. /* adapter came with a sas address */
  23. if (asd_ha->hw_prof.sas_addr[0])
  24. return 0;
  25. return sas_request_addr(asd_ha->sas_ha.core.shost,
  26. asd_ha->hw_prof.sas_addr);
  27. }
  28. static void asd_propagate_sas_addr(struct asd_ha_struct *asd_ha)
  29. {
  30. int i;
  31. for (i = 0; i < ASD_MAX_PHYS; i++) {
  32. if (asd_ha->hw_prof.phy_desc[i].sas_addr[0] == 0)
  33. continue;
  34. /* Set a phy's address only if it has none.
  35. */
  36. ASD_DPRINTK("setting phy%d addr to %llx\n", i,
  37. SAS_ADDR(asd_ha->hw_prof.sas_addr));
  38. memcpy(asd_ha->hw_prof.phy_desc[i].sas_addr,
  39. asd_ha->hw_prof.sas_addr, SAS_ADDR_SIZE);
  40. }
  41. }
  42. /* ---------- PHY initialization ---------- */
  43. static void asd_init_phy_identify(struct asd_phy *phy)
  44. {
  45. phy->identify_frame = phy->id_frm_tok->vaddr;
  46. memset(phy->identify_frame, 0, sizeof(*phy->identify_frame));
  47. phy->identify_frame->dev_type = SAS_END_DEVICE;
  48. if (phy->sas_phy.role & PHY_ROLE_INITIATOR)
  49. phy->identify_frame->initiator_bits = phy->sas_phy.iproto;
  50. if (phy->sas_phy.role & PHY_ROLE_TARGET)
  51. phy->identify_frame->target_bits = phy->sas_phy.tproto;
  52. memcpy(phy->identify_frame->sas_addr, phy->phy_desc->sas_addr,
  53. SAS_ADDR_SIZE);
  54. phy->identify_frame->phy_id = phy->sas_phy.id;
  55. }
  56. static int asd_init_phy(struct asd_phy *phy)
  57. {
  58. struct asd_ha_struct *asd_ha = phy->sas_phy.ha->lldd_ha;
  59. struct asd_sas_phy *sas_phy = &phy->sas_phy;
  60. sas_phy->enabled = 1;
  61. sas_phy->class = SAS;
  62. sas_phy->iproto = SAS_PROTOCOL_ALL;
  63. sas_phy->tproto = 0;
  64. sas_phy->type = PHY_TYPE_PHYSICAL;
  65. sas_phy->role = PHY_ROLE_INITIATOR;
  66. sas_phy->oob_mode = OOB_NOT_CONNECTED;
  67. sas_phy->linkrate = SAS_LINK_RATE_UNKNOWN;
  68. phy->id_frm_tok = asd_alloc_coherent(asd_ha,
  69. sizeof(*phy->identify_frame),
  70. GFP_KERNEL);
  71. if (!phy->id_frm_tok) {
  72. asd_printk("no mem for IDENTIFY for phy%d\n", sas_phy->id);
  73. return -ENOMEM;
  74. } else
  75. asd_init_phy_identify(phy);
  76. memset(phy->frame_rcvd, 0, sizeof(phy->frame_rcvd));
  77. return 0;
  78. }
  79. static void asd_init_ports(struct asd_ha_struct *asd_ha)
  80. {
  81. int i;
  82. spin_lock_init(&asd_ha->asd_ports_lock);
  83. for (i = 0; i < ASD_MAX_PHYS; i++) {
  84. struct asd_port *asd_port = &asd_ha->asd_ports[i];
  85. memset(asd_port->sas_addr, 0, SAS_ADDR_SIZE);
  86. memset(asd_port->attached_sas_addr, 0, SAS_ADDR_SIZE);
  87. asd_port->phy_mask = 0;
  88. asd_port->num_phys = 0;
  89. }
  90. }
  91. static int asd_init_phys(struct asd_ha_struct *asd_ha)
  92. {
  93. u8 i;
  94. u8 phy_mask = asd_ha->hw_prof.enabled_phys;
  95. for (i = 0; i < ASD_MAX_PHYS; i++) {
  96. struct asd_phy *phy = &asd_ha->phys[i];
  97. phy->phy_desc = &asd_ha->hw_prof.phy_desc[i];
  98. phy->asd_port = NULL;
  99. phy->sas_phy.enabled = 0;
  100. phy->sas_phy.id = i;
  101. phy->sas_phy.sas_addr = &phy->phy_desc->sas_addr[0];
  102. phy->sas_phy.frame_rcvd = &phy->frame_rcvd[0];
  103. phy->sas_phy.ha = &asd_ha->sas_ha;
  104. phy->sas_phy.lldd_phy = phy;
  105. }
  106. /* Now enable and initialize only the enabled phys. */
  107. for_each_phy(phy_mask, phy_mask, i) {
  108. int err = asd_init_phy(&asd_ha->phys[i]);
  109. if (err)
  110. return err;
  111. }
  112. return 0;
  113. }
  114. /* ---------- Sliding windows ---------- */
  115. static int asd_init_sw(struct asd_ha_struct *asd_ha)
  116. {
  117. struct pci_dev *pcidev = asd_ha->pcidev;
  118. int err;
  119. u32 v;
  120. /* Unlock MBARs */
  121. err = pci_read_config_dword(pcidev, PCI_CONF_MBAR_KEY, &v);
  122. if (err) {
  123. asd_printk("couldn't access conf. space of %s\n",
  124. pci_name(pcidev));
  125. goto Err;
  126. }
  127. if (v)
  128. err = pci_write_config_dword(pcidev, PCI_CONF_MBAR_KEY, v);
  129. if (err) {
  130. asd_printk("couldn't write to MBAR_KEY of %s\n",
  131. pci_name(pcidev));
  132. goto Err;
  133. }
  134. /* Set sliding windows A, B and C to point to proper internal
  135. * memory regions.
  136. */
  137. pci_write_config_dword(pcidev, PCI_CONF_MBAR0_SWA, REG_BASE_ADDR);
  138. pci_write_config_dword(pcidev, PCI_CONF_MBAR0_SWB,
  139. REG_BASE_ADDR_CSEQCIO);
  140. pci_write_config_dword(pcidev, PCI_CONF_MBAR0_SWC, REG_BASE_ADDR_EXSI);
  141. asd_ha->io_handle[0].swa_base = REG_BASE_ADDR;
  142. asd_ha->io_handle[0].swb_base = REG_BASE_ADDR_CSEQCIO;
  143. asd_ha->io_handle[0].swc_base = REG_BASE_ADDR_EXSI;
  144. MBAR0_SWB_SIZE = asd_ha->io_handle[0].len - 0x80;
  145. if (!asd_ha->iospace) {
  146. /* MBAR1 will point to OCM (On Chip Memory) */
  147. pci_write_config_dword(pcidev, PCI_CONF_MBAR1, OCM_BASE_ADDR);
  148. asd_ha->io_handle[1].swa_base = OCM_BASE_ADDR;
  149. }
  150. spin_lock_init(&asd_ha->iolock);
  151. Err:
  152. return err;
  153. }
  154. /* ---------- SCB initialization ---------- */
  155. /**
  156. * asd_init_scbs - manually allocate the first SCB.
  157. * @asd_ha: pointer to host adapter structure
  158. *
  159. * This allocates the very first SCB which would be sent to the
  160. * sequencer for execution. Its bus address is written to
  161. * CSEQ_Q_NEW_POINTER, mode page 2, mode 8. Since the bus address of
  162. * the _next_ scb to be DMA-ed to the host adapter is read from the last
  163. * SCB DMA-ed to the host adapter, we have to always stay one step
  164. * ahead of the sequencer and keep one SCB already allocated.
  165. */
  166. static int asd_init_scbs(struct asd_ha_struct *asd_ha)
  167. {
  168. struct asd_seq_data *seq = &asd_ha->seq;
  169. int bitmap_bytes;
  170. /* allocate the index array and bitmap */
  171. asd_ha->seq.tc_index_bitmap_bits = asd_ha->hw_prof.max_scbs;
  172. asd_ha->seq.tc_index_array = kcalloc(asd_ha->seq.tc_index_bitmap_bits,
  173. sizeof(void *),
  174. GFP_KERNEL);
  175. if (!asd_ha->seq.tc_index_array)
  176. return -ENOMEM;
  177. bitmap_bytes = (asd_ha->seq.tc_index_bitmap_bits+7)/8;
  178. bitmap_bytes = BITS_TO_LONGS(bitmap_bytes*8)*sizeof(unsigned long);
  179. asd_ha->seq.tc_index_bitmap = kzalloc(bitmap_bytes, GFP_KERNEL);
  180. if (!asd_ha->seq.tc_index_bitmap) {
  181. kfree(asd_ha->seq.tc_index_array);
  182. asd_ha->seq.tc_index_array = NULL;
  183. return -ENOMEM;
  184. }
  185. spin_lock_init(&seq->tc_index_lock);
  186. seq->next_scb.size = sizeof(struct scb);
  187. seq->next_scb.vaddr = dma_pool_alloc(asd_ha->scb_pool, GFP_KERNEL,
  188. &seq->next_scb.dma_handle);
  189. if (!seq->next_scb.vaddr) {
  190. kfree(asd_ha->seq.tc_index_bitmap);
  191. kfree(asd_ha->seq.tc_index_array);
  192. asd_ha->seq.tc_index_bitmap = NULL;
  193. asd_ha->seq.tc_index_array = NULL;
  194. return -ENOMEM;
  195. }
  196. seq->pending = 0;
  197. spin_lock_init(&seq->pend_q_lock);
  198. INIT_LIST_HEAD(&seq->pend_q);
  199. return 0;
  200. }
  201. static void asd_get_max_scb_ddb(struct asd_ha_struct *asd_ha)
  202. {
  203. asd_ha->hw_prof.max_scbs = asd_get_cmdctx_size(asd_ha)/ASD_SCB_SIZE;
  204. asd_ha->hw_prof.max_ddbs = asd_get_devctx_size(asd_ha)/ASD_DDB_SIZE;
  205. ASD_DPRINTK("max_scbs:%d, max_ddbs:%d\n",
  206. asd_ha->hw_prof.max_scbs,
  207. asd_ha->hw_prof.max_ddbs);
  208. }
  209. /* ---------- Done List initialization ---------- */
  210. static void asd_dl_tasklet_handler(unsigned long);
  211. static int asd_init_dl(struct asd_ha_struct *asd_ha)
  212. {
  213. asd_ha->seq.actual_dl
  214. = asd_alloc_coherent(asd_ha,
  215. ASD_DL_SIZE * sizeof(struct done_list_struct),
  216. GFP_KERNEL);
  217. if (!asd_ha->seq.actual_dl)
  218. return -ENOMEM;
  219. asd_ha->seq.dl = asd_ha->seq.actual_dl->vaddr;
  220. asd_ha->seq.dl_toggle = ASD_DEF_DL_TOGGLE;
  221. asd_ha->seq.dl_next = 0;
  222. tasklet_init(&asd_ha->seq.dl_tasklet, asd_dl_tasklet_handler,
  223. (unsigned long) asd_ha);
  224. return 0;
  225. }
  226. /* ---------- EDB and ESCB init ---------- */
  227. static int asd_alloc_edbs(struct asd_ha_struct *asd_ha, gfp_t gfp_flags)
  228. {
  229. struct asd_seq_data *seq = &asd_ha->seq;
  230. int i;
  231. seq->edb_arr = kmalloc_array(seq->num_edbs, sizeof(*seq->edb_arr),
  232. gfp_flags);
  233. if (!seq->edb_arr)
  234. return -ENOMEM;
  235. for (i = 0; i < seq->num_edbs; i++) {
  236. seq->edb_arr[i] = asd_alloc_coherent(asd_ha, ASD_EDB_SIZE,
  237. gfp_flags);
  238. if (!seq->edb_arr[i])
  239. goto Err_unroll;
  240. memset(seq->edb_arr[i]->vaddr, 0, ASD_EDB_SIZE);
  241. }
  242. ASD_DPRINTK("num_edbs:%d\n", seq->num_edbs);
  243. return 0;
  244. Err_unroll:
  245. for (i-- ; i >= 0; i--)
  246. asd_free_coherent(asd_ha, seq->edb_arr[i]);
  247. kfree(seq->edb_arr);
  248. seq->edb_arr = NULL;
  249. return -ENOMEM;
  250. }
  251. static int asd_alloc_escbs(struct asd_ha_struct *asd_ha,
  252. gfp_t gfp_flags)
  253. {
  254. struct asd_seq_data *seq = &asd_ha->seq;
  255. struct asd_ascb *escb;
  256. int i, escbs;
  257. seq->escb_arr = kmalloc_array(seq->num_escbs, sizeof(*seq->escb_arr),
  258. gfp_flags);
  259. if (!seq->escb_arr)
  260. return -ENOMEM;
  261. escbs = seq->num_escbs;
  262. escb = asd_ascb_alloc_list(asd_ha, &escbs, gfp_flags);
  263. if (!escb) {
  264. asd_printk("couldn't allocate list of escbs\n");
  265. goto Err;
  266. }
  267. seq->num_escbs -= escbs; /* subtract what was not allocated */
  268. ASD_DPRINTK("num_escbs:%d\n", seq->num_escbs);
  269. for (i = 0; i < seq->num_escbs; i++, escb = list_entry(escb->list.next,
  270. struct asd_ascb,
  271. list)) {
  272. seq->escb_arr[i] = escb;
  273. escb->scb->header.opcode = EMPTY_SCB;
  274. }
  275. return 0;
  276. Err:
  277. kfree(seq->escb_arr);
  278. seq->escb_arr = NULL;
  279. return -ENOMEM;
  280. }
  281. static void asd_assign_edbs2escbs(struct asd_ha_struct *asd_ha)
  282. {
  283. struct asd_seq_data *seq = &asd_ha->seq;
  284. int i, k, z = 0;
  285. for (i = 0; i < seq->num_escbs; i++) {
  286. struct asd_ascb *ascb = seq->escb_arr[i];
  287. struct empty_scb *escb = &ascb->scb->escb;
  288. ascb->edb_index = z;
  289. escb->num_valid = ASD_EDBS_PER_SCB;
  290. for (k = 0; k < ASD_EDBS_PER_SCB; k++) {
  291. struct sg_el *eb = &escb->eb[k];
  292. struct asd_dma_tok *edb = seq->edb_arr[z++];
  293. memset(eb, 0, sizeof(*eb));
  294. eb->bus_addr = cpu_to_le64(((u64) edb->dma_handle));
  295. eb->size = cpu_to_le32(((u32) edb->size));
  296. }
  297. }
  298. }
  299. /**
  300. * asd_init_escbs -- allocate and initialize empty scbs
  301. * @asd_ha: pointer to host adapter structure
  302. *
  303. * An empty SCB has sg_elements of ASD_EDBS_PER_SCB (7) buffers.
  304. * They transport sense data, etc.
  305. */
  306. static int asd_init_escbs(struct asd_ha_struct *asd_ha)
  307. {
  308. struct asd_seq_data *seq = &asd_ha->seq;
  309. int err = 0;
  310. /* Allocate two empty data buffers (edb) per sequencer. */
  311. int edbs = 2*(1+asd_ha->hw_prof.num_phys);
  312. seq->num_escbs = (edbs+ASD_EDBS_PER_SCB-1)/ASD_EDBS_PER_SCB;
  313. seq->num_edbs = seq->num_escbs * ASD_EDBS_PER_SCB;
  314. err = asd_alloc_edbs(asd_ha, GFP_KERNEL);
  315. if (err) {
  316. asd_printk("couldn't allocate edbs\n");
  317. return err;
  318. }
  319. err = asd_alloc_escbs(asd_ha, GFP_KERNEL);
  320. if (err) {
  321. asd_printk("couldn't allocate escbs\n");
  322. return err;
  323. }
  324. asd_assign_edbs2escbs(asd_ha);
  325. /* In order to insure that normal SCBs do not overfill sequencer
  326. * memory and leave no space for escbs (halting condition),
  327. * we increment pending here by the number of escbs. However,
  328. * escbs are never pending.
  329. */
  330. seq->pending = seq->num_escbs;
  331. seq->can_queue = 1 + (asd_ha->hw_prof.max_scbs - seq->pending)/2;
  332. return 0;
  333. }
  334. /* ---------- HW initialization ---------- */
  335. /**
  336. * asd_chip_hardrst -- hard reset the chip
  337. * @asd_ha: pointer to host adapter structure
  338. *
  339. * This takes 16 cycles and is synchronous to CFCLK, which runs
  340. * at 200 MHz, so this should take at most 80 nanoseconds.
  341. */
  342. int asd_chip_hardrst(struct asd_ha_struct *asd_ha)
  343. {
  344. int i;
  345. int count = 100;
  346. u32 reg;
  347. for (i = 0 ; i < 4 ; i++) {
  348. asd_write_reg_dword(asd_ha, COMBIST, HARDRST);
  349. }
  350. do {
  351. udelay(1);
  352. reg = asd_read_reg_dword(asd_ha, CHIMINT);
  353. if (reg & HARDRSTDET) {
  354. asd_write_reg_dword(asd_ha, CHIMINT,
  355. HARDRSTDET|PORRSTDET);
  356. return 0;
  357. }
  358. } while (--count > 0);
  359. return -ENODEV;
  360. }
  361. /**
  362. * asd_init_chip -- initialize the chip
  363. * @asd_ha: pointer to host adapter structure
  364. *
  365. * Hard resets the chip, disables HA interrupts, downloads the sequnecer
  366. * microcode and starts the sequencers. The caller has to explicitly
  367. * enable HA interrupts with asd_enable_ints(asd_ha).
  368. */
  369. static int asd_init_chip(struct asd_ha_struct *asd_ha)
  370. {
  371. int err;
  372. err = asd_chip_hardrst(asd_ha);
  373. if (err) {
  374. asd_printk("couldn't hard reset %s\n",
  375. pci_name(asd_ha->pcidev));
  376. goto out;
  377. }
  378. asd_disable_ints(asd_ha);
  379. err = asd_init_seqs(asd_ha);
  380. if (err) {
  381. asd_printk("couldn't init seqs for %s\n",
  382. pci_name(asd_ha->pcidev));
  383. goto out;
  384. }
  385. err = asd_start_seqs(asd_ha);
  386. if (err) {
  387. asd_printk("couldn't start seqs for %s\n",
  388. pci_name(asd_ha->pcidev));
  389. goto out;
  390. }
  391. out:
  392. return err;
  393. }
  394. #define MAX_DEVS ((OCM_MAX_SIZE) / (ASD_DDB_SIZE))
  395. static int max_devs = 0;
  396. module_param_named(max_devs, max_devs, int, S_IRUGO);
  397. MODULE_PARM_DESC(max_devs, "\n"
  398. "\tMaximum number of SAS devices to support (not LUs).\n"
  399. "\tDefault: 2176, Maximum: 65663.\n");
  400. static int max_cmnds = 0;
  401. module_param_named(max_cmnds, max_cmnds, int, S_IRUGO);
  402. MODULE_PARM_DESC(max_cmnds, "\n"
  403. "\tMaximum number of commands queuable.\n"
  404. "\tDefault: 512, Maximum: 66047.\n");
  405. static void asd_extend_devctx_ocm(struct asd_ha_struct *asd_ha)
  406. {
  407. unsigned long dma_addr = OCM_BASE_ADDR;
  408. u32 d;
  409. dma_addr -= asd_ha->hw_prof.max_ddbs * ASD_DDB_SIZE;
  410. asd_write_reg_addr(asd_ha, DEVCTXBASE, (dma_addr_t) dma_addr);
  411. d = asd_read_reg_dword(asd_ha, CTXDOMAIN);
  412. d |= 4;
  413. asd_write_reg_dword(asd_ha, CTXDOMAIN, d);
  414. asd_ha->hw_prof.max_ddbs += MAX_DEVS;
  415. }
  416. static int asd_extend_devctx(struct asd_ha_struct *asd_ha)
  417. {
  418. dma_addr_t dma_handle;
  419. unsigned long dma_addr;
  420. u32 d;
  421. int size;
  422. asd_extend_devctx_ocm(asd_ha);
  423. asd_ha->hw_prof.ddb_ext = NULL;
  424. if (max_devs <= asd_ha->hw_prof.max_ddbs || max_devs > 0xFFFF) {
  425. max_devs = asd_ha->hw_prof.max_ddbs;
  426. return 0;
  427. }
  428. size = (max_devs - asd_ha->hw_prof.max_ddbs + 1) * ASD_DDB_SIZE;
  429. asd_ha->hw_prof.ddb_ext = asd_alloc_coherent(asd_ha, size, GFP_KERNEL);
  430. if (!asd_ha->hw_prof.ddb_ext) {
  431. asd_printk("couldn't allocate memory for %d devices\n",
  432. max_devs);
  433. max_devs = asd_ha->hw_prof.max_ddbs;
  434. return -ENOMEM;
  435. }
  436. dma_handle = asd_ha->hw_prof.ddb_ext->dma_handle;
  437. dma_addr = ALIGN((unsigned long) dma_handle, ASD_DDB_SIZE);
  438. dma_addr -= asd_ha->hw_prof.max_ddbs * ASD_DDB_SIZE;
  439. dma_handle = (dma_addr_t) dma_addr;
  440. asd_write_reg_addr(asd_ha, DEVCTXBASE, dma_handle);
  441. d = asd_read_reg_dword(asd_ha, CTXDOMAIN);
  442. d &= ~4;
  443. asd_write_reg_dword(asd_ha, CTXDOMAIN, d);
  444. asd_ha->hw_prof.max_ddbs = max_devs;
  445. return 0;
  446. }
  447. static int asd_extend_cmdctx(struct asd_ha_struct *asd_ha)
  448. {
  449. dma_addr_t dma_handle;
  450. unsigned long dma_addr;
  451. u32 d;
  452. int size;
  453. asd_ha->hw_prof.scb_ext = NULL;
  454. if (max_cmnds <= asd_ha->hw_prof.max_scbs || max_cmnds > 0xFFFF) {
  455. max_cmnds = asd_ha->hw_prof.max_scbs;
  456. return 0;
  457. }
  458. size = (max_cmnds - asd_ha->hw_prof.max_scbs + 1) * ASD_SCB_SIZE;
  459. asd_ha->hw_prof.scb_ext = asd_alloc_coherent(asd_ha, size, GFP_KERNEL);
  460. if (!asd_ha->hw_prof.scb_ext) {
  461. asd_printk("couldn't allocate memory for %d commands\n",
  462. max_cmnds);
  463. max_cmnds = asd_ha->hw_prof.max_scbs;
  464. return -ENOMEM;
  465. }
  466. dma_handle = asd_ha->hw_prof.scb_ext->dma_handle;
  467. dma_addr = ALIGN((unsigned long) dma_handle, ASD_SCB_SIZE);
  468. dma_addr -= asd_ha->hw_prof.max_scbs * ASD_SCB_SIZE;
  469. dma_handle = (dma_addr_t) dma_addr;
  470. asd_write_reg_addr(asd_ha, CMDCTXBASE, dma_handle);
  471. d = asd_read_reg_dword(asd_ha, CTXDOMAIN);
  472. d &= ~1;
  473. asd_write_reg_dword(asd_ha, CTXDOMAIN, d);
  474. asd_ha->hw_prof.max_scbs = max_cmnds;
  475. return 0;
  476. }
  477. /**
  478. * asd_init_ctxmem -- initialize context memory
  479. * @asd_ha: pointer to host adapter structure
  480. *
  481. * This function sets the maximum number of SCBs and
  482. * DDBs which can be used by the sequencer. This is normally
  483. * 512 and 128 respectively. If support for more SCBs or more DDBs
  484. * is required then CMDCTXBASE, DEVCTXBASE and CTXDOMAIN are
  485. * initialized here to extend context memory to point to host memory,
  486. * thus allowing unlimited support for SCBs and DDBs -- only limited
  487. * by host memory.
  488. */
  489. static int asd_init_ctxmem(struct asd_ha_struct *asd_ha)
  490. {
  491. int bitmap_bytes;
  492. asd_get_max_scb_ddb(asd_ha);
  493. asd_extend_devctx(asd_ha);
  494. asd_extend_cmdctx(asd_ha);
  495. /* The kernel wants bitmaps to be unsigned long sized. */
  496. bitmap_bytes = (asd_ha->hw_prof.max_ddbs+7)/8;
  497. bitmap_bytes = BITS_TO_LONGS(bitmap_bytes*8)*sizeof(unsigned long);
  498. asd_ha->hw_prof.ddb_bitmap = kzalloc(bitmap_bytes, GFP_KERNEL);
  499. if (!asd_ha->hw_prof.ddb_bitmap)
  500. return -ENOMEM;
  501. spin_lock_init(&asd_ha->hw_prof.ddb_lock);
  502. return 0;
  503. }
  504. int asd_init_hw(struct asd_ha_struct *asd_ha)
  505. {
  506. int err;
  507. u32 v;
  508. err = asd_init_sw(asd_ha);
  509. if (err)
  510. return err;
  511. err = pci_read_config_dword(asd_ha->pcidev, PCIC_HSTPCIX_CNTRL, &v);
  512. if (err) {
  513. asd_printk("couldn't read PCIC_HSTPCIX_CNTRL of %s\n",
  514. pci_name(asd_ha->pcidev));
  515. return err;
  516. }
  517. err = pci_write_config_dword(asd_ha->pcidev, PCIC_HSTPCIX_CNTRL,
  518. v | SC_TMR_DIS);
  519. if (err) {
  520. asd_printk("couldn't disable split completion timer of %s\n",
  521. pci_name(asd_ha->pcidev));
  522. return err;
  523. }
  524. err = asd_read_ocm(asd_ha);
  525. if (err) {
  526. asd_printk("couldn't read ocm(%d)\n", err);
  527. /* While suspicios, it is not an error that we
  528. * couldn't read the OCM. */
  529. }
  530. err = asd_read_flash(asd_ha);
  531. if (err) {
  532. asd_printk("couldn't read flash(%d)\n", err);
  533. /* While suspicios, it is not an error that we
  534. * couldn't read FLASH memory.
  535. */
  536. }
  537. asd_init_ctxmem(asd_ha);
  538. if (asd_get_user_sas_addr(asd_ha)) {
  539. asd_printk("No SAS Address provided for %s\n",
  540. pci_name(asd_ha->pcidev));
  541. err = -ENODEV;
  542. goto Out;
  543. }
  544. asd_propagate_sas_addr(asd_ha);
  545. err = asd_init_phys(asd_ha);
  546. if (err) {
  547. asd_printk("couldn't initialize phys for %s\n",
  548. pci_name(asd_ha->pcidev));
  549. goto Out;
  550. }
  551. asd_init_ports(asd_ha);
  552. err = asd_init_scbs(asd_ha);
  553. if (err) {
  554. asd_printk("couldn't initialize scbs for %s\n",
  555. pci_name(asd_ha->pcidev));
  556. goto Out;
  557. }
  558. err = asd_init_dl(asd_ha);
  559. if (err) {
  560. asd_printk("couldn't initialize the done list:%d\n",
  561. err);
  562. goto Out;
  563. }
  564. err = asd_init_escbs(asd_ha);
  565. if (err) {
  566. asd_printk("couldn't initialize escbs\n");
  567. goto Out;
  568. }
  569. err = asd_init_chip(asd_ha);
  570. if (err) {
  571. asd_printk("couldn't init the chip\n");
  572. goto Out;
  573. }
  574. Out:
  575. return err;
  576. }
  577. /* ---------- Chip reset ---------- */
  578. /**
  579. * asd_chip_reset -- reset the host adapter, etc
  580. * @asd_ha: pointer to host adapter structure of interest
  581. *
  582. * Called from the ISR. Hard reset the chip. Let everything
  583. * timeout. This should be no different than hot-unplugging the
  584. * host adapter. Once everything times out we'll init the chip with
  585. * a call to asd_init_chip() and enable interrupts with asd_enable_ints().
  586. * XXX finish.
  587. */
  588. static void asd_chip_reset(struct asd_ha_struct *asd_ha)
  589. {
  590. ASD_DPRINTK("chip reset for %s\n", pci_name(asd_ha->pcidev));
  591. asd_chip_hardrst(asd_ha);
  592. }
  593. /* ---------- Done List Routines ---------- */
  594. static void asd_dl_tasklet_handler(unsigned long data)
  595. {
  596. struct asd_ha_struct *asd_ha = (struct asd_ha_struct *) data;
  597. struct asd_seq_data *seq = &asd_ha->seq;
  598. unsigned long flags;
  599. while (1) {
  600. struct done_list_struct *dl = &seq->dl[seq->dl_next];
  601. struct asd_ascb *ascb;
  602. if ((dl->toggle & DL_TOGGLE_MASK) != seq->dl_toggle)
  603. break;
  604. /* find the aSCB */
  605. spin_lock_irqsave(&seq->tc_index_lock, flags);
  606. ascb = asd_tc_index_find(seq, (int)le16_to_cpu(dl->index));
  607. spin_unlock_irqrestore(&seq->tc_index_lock, flags);
  608. if (unlikely(!ascb)) {
  609. ASD_DPRINTK("BUG:sequencer:dl:no ascb?!\n");
  610. goto next_1;
  611. } else if (ascb->scb->header.opcode == EMPTY_SCB) {
  612. goto out;
  613. } else if (!ascb->uldd_timer && !del_timer(&ascb->timer)) {
  614. goto next_1;
  615. }
  616. spin_lock_irqsave(&seq->pend_q_lock, flags);
  617. list_del_init(&ascb->list);
  618. seq->pending--;
  619. spin_unlock_irqrestore(&seq->pend_q_lock, flags);
  620. out:
  621. ascb->tasklet_complete(ascb, dl);
  622. next_1:
  623. seq->dl_next = (seq->dl_next + 1) & (ASD_DL_SIZE-1);
  624. if (!seq->dl_next)
  625. seq->dl_toggle ^= DL_TOGGLE_MASK;
  626. }
  627. }
  628. /* ---------- Interrupt Service Routines ---------- */
  629. /**
  630. * asd_process_donelist_isr -- schedule processing of done list entries
  631. * @asd_ha: pointer to host adapter structure
  632. */
  633. static void asd_process_donelist_isr(struct asd_ha_struct *asd_ha)
  634. {
  635. tasklet_schedule(&asd_ha->seq.dl_tasklet);
  636. }
  637. /**
  638. * asd_com_sas_isr -- process device communication interrupt (COMINT)
  639. * @asd_ha: pointer to host adapter structure
  640. */
  641. static void asd_com_sas_isr(struct asd_ha_struct *asd_ha)
  642. {
  643. u32 comstat = asd_read_reg_dword(asd_ha, COMSTAT);
  644. /* clear COMSTAT int */
  645. asd_write_reg_dword(asd_ha, COMSTAT, 0xFFFFFFFF);
  646. if (comstat & CSBUFPERR) {
  647. asd_printk("%s: command/status buffer dma parity error\n",
  648. pci_name(asd_ha->pcidev));
  649. } else if (comstat & CSERR) {
  650. int i;
  651. u32 dmaerr = asd_read_reg_dword(asd_ha, DMAERR);
  652. dmaerr &= 0xFF;
  653. asd_printk("%s: command/status dma error, DMAERR: 0x%02x, "
  654. "CSDMAADR: 0x%04x, CSDMAADR+4: 0x%04x\n",
  655. pci_name(asd_ha->pcidev),
  656. dmaerr,
  657. asd_read_reg_dword(asd_ha, CSDMAADR),
  658. asd_read_reg_dword(asd_ha, CSDMAADR+4));
  659. asd_printk("CSBUFFER:\n");
  660. for (i = 0; i < 8; i++) {
  661. asd_printk("%08x %08x %08x %08x\n",
  662. asd_read_reg_dword(asd_ha, CSBUFFER),
  663. asd_read_reg_dword(asd_ha, CSBUFFER+4),
  664. asd_read_reg_dword(asd_ha, CSBUFFER+8),
  665. asd_read_reg_dword(asd_ha, CSBUFFER+12));
  666. }
  667. asd_dump_seq_state(asd_ha, 0);
  668. } else if (comstat & OVLYERR) {
  669. u32 dmaerr = asd_read_reg_dword(asd_ha, DMAERR);
  670. dmaerr = (dmaerr >> 8) & 0xFF;
  671. asd_printk("%s: overlay dma error:0x%x\n",
  672. pci_name(asd_ha->pcidev),
  673. dmaerr);
  674. }
  675. asd_chip_reset(asd_ha);
  676. }
  677. static void asd_arp2_err(struct asd_ha_struct *asd_ha, u32 dchstatus)
  678. {
  679. static const char *halt_code[256] = {
  680. "UNEXPECTED_INTERRUPT0",
  681. "UNEXPECTED_INTERRUPT1",
  682. "UNEXPECTED_INTERRUPT2",
  683. "UNEXPECTED_INTERRUPT3",
  684. "UNEXPECTED_INTERRUPT4",
  685. "UNEXPECTED_INTERRUPT5",
  686. "UNEXPECTED_INTERRUPT6",
  687. "UNEXPECTED_INTERRUPT7",
  688. "UNEXPECTED_INTERRUPT8",
  689. "UNEXPECTED_INTERRUPT9",
  690. "UNEXPECTED_INTERRUPT10",
  691. [11 ... 19] = "unknown[11,19]",
  692. "NO_FREE_SCB_AVAILABLE",
  693. "INVALID_SCB_OPCODE",
  694. "INVALID_MBX_OPCODE",
  695. "INVALID_ATA_STATE",
  696. "ATA_QUEUE_FULL",
  697. "ATA_TAG_TABLE_FAULT",
  698. "ATA_TAG_MASK_FAULT",
  699. "BAD_LINK_QUEUE_STATE",
  700. "DMA2CHIM_QUEUE_ERROR",
  701. "EMPTY_SCB_LIST_FULL",
  702. "unknown[30]",
  703. "IN_USE_SCB_ON_FREE_LIST",
  704. "BAD_OPEN_WAIT_STATE",
  705. "INVALID_STP_AFFILIATION",
  706. "unknown[34]",
  707. "EXEC_QUEUE_ERROR",
  708. "TOO_MANY_EMPTIES_NEEDED",
  709. "EMPTY_REQ_QUEUE_ERROR",
  710. "Q_MONIRTT_MGMT_ERROR",
  711. "TARGET_MODE_FLOW_ERROR",
  712. "DEVICE_QUEUE_NOT_FOUND",
  713. "START_IRTT_TIMER_ERROR",
  714. "ABORT_TASK_ILLEGAL_REQ",
  715. [43 ... 255] = "unknown[43,255]"
  716. };
  717. if (dchstatus & CSEQINT) {
  718. u32 arp2int = asd_read_reg_dword(asd_ha, CARP2INT);
  719. if (arp2int & (ARP2WAITTO|ARP2ILLOPC|ARP2PERR|ARP2CIOPERR)) {
  720. asd_printk("%s: CSEQ arp2int:0x%x\n",
  721. pci_name(asd_ha->pcidev),
  722. arp2int);
  723. } else if (arp2int & ARP2HALTC)
  724. asd_printk("%s: CSEQ halted: %s\n",
  725. pci_name(asd_ha->pcidev),
  726. halt_code[(arp2int>>16)&0xFF]);
  727. else
  728. asd_printk("%s: CARP2INT:0x%x\n",
  729. pci_name(asd_ha->pcidev),
  730. arp2int);
  731. }
  732. if (dchstatus & LSEQINT_MASK) {
  733. int lseq;
  734. u8 lseq_mask = dchstatus & LSEQINT_MASK;
  735. for_each_sequencer(lseq_mask, lseq_mask, lseq) {
  736. u32 arp2int = asd_read_reg_dword(asd_ha,
  737. LmARP2INT(lseq));
  738. if (arp2int & (ARP2WAITTO | ARP2ILLOPC | ARP2PERR
  739. | ARP2CIOPERR)) {
  740. asd_printk("%s: LSEQ%d arp2int:0x%x\n",
  741. pci_name(asd_ha->pcidev),
  742. lseq, arp2int);
  743. /* XXX we should only do lseq reset */
  744. } else if (arp2int & ARP2HALTC)
  745. asd_printk("%s: LSEQ%d halted: %s\n",
  746. pci_name(asd_ha->pcidev),
  747. lseq,halt_code[(arp2int>>16)&0xFF]);
  748. else
  749. asd_printk("%s: LSEQ%d ARP2INT:0x%x\n",
  750. pci_name(asd_ha->pcidev), lseq,
  751. arp2int);
  752. }
  753. }
  754. asd_chip_reset(asd_ha);
  755. }
  756. /**
  757. * asd_dch_sas_isr -- process device channel interrupt (DEVINT)
  758. * @asd_ha: pointer to host adapter structure
  759. */
  760. static void asd_dch_sas_isr(struct asd_ha_struct *asd_ha)
  761. {
  762. u32 dchstatus = asd_read_reg_dword(asd_ha, DCHSTATUS);
  763. if (dchstatus & CFIFTOERR) {
  764. asd_printk("%s: CFIFTOERR\n", pci_name(asd_ha->pcidev));
  765. asd_chip_reset(asd_ha);
  766. } else
  767. asd_arp2_err(asd_ha, dchstatus);
  768. }
  769. /**
  770. * asd_rbi_exsi_isr -- process external system interface interrupt (INITERR)
  771. * @asd_ha: pointer to host adapter structure
  772. */
  773. static void asd_rbi_exsi_isr(struct asd_ha_struct *asd_ha)
  774. {
  775. u32 stat0r = asd_read_reg_dword(asd_ha, ASISTAT0R);
  776. if (!(stat0r & ASIERR)) {
  777. asd_printk("hmm, EXSI interrupted but no error?\n");
  778. return;
  779. }
  780. if (stat0r & ASIFMTERR) {
  781. asd_printk("ASI SEEPROM format error for %s\n",
  782. pci_name(asd_ha->pcidev));
  783. } else if (stat0r & ASISEECHKERR) {
  784. u32 stat1r = asd_read_reg_dword(asd_ha, ASISTAT1R);
  785. asd_printk("ASI SEEPROM checksum 0x%x error for %s\n",
  786. stat1r & CHECKSUM_MASK,
  787. pci_name(asd_ha->pcidev));
  788. } else {
  789. u32 statr = asd_read_reg_dword(asd_ha, ASIERRSTATR);
  790. if (!(statr & CPI2ASIMSTERR_MASK)) {
  791. ASD_DPRINTK("hmm, ASIERR?\n");
  792. return;
  793. } else {
  794. u32 addr = asd_read_reg_dword(asd_ha, ASIERRADDR);
  795. u32 data = asd_read_reg_dword(asd_ha, ASIERRDATAR);
  796. asd_printk("%s: CPI2 xfer err: addr: 0x%x, wdata: 0x%x, "
  797. "count: 0x%x, byteen: 0x%x, targerr: 0x%x "
  798. "master id: 0x%x, master err: 0x%x\n",
  799. pci_name(asd_ha->pcidev),
  800. addr, data,
  801. (statr & CPI2ASIBYTECNT_MASK) >> 16,
  802. (statr & CPI2ASIBYTEEN_MASK) >> 12,
  803. (statr & CPI2ASITARGERR_MASK) >> 8,
  804. (statr & CPI2ASITARGMID_MASK) >> 4,
  805. (statr & CPI2ASIMSTERR_MASK));
  806. }
  807. }
  808. asd_chip_reset(asd_ha);
  809. }
  810. /**
  811. * asd_hst_pcix_isr -- process host interface interrupts
  812. * @asd_ha: pointer to host adapter structure
  813. *
  814. * Asserted on PCIX errors: target abort, etc.
  815. */
  816. static void asd_hst_pcix_isr(struct asd_ha_struct *asd_ha)
  817. {
  818. u16 status;
  819. u32 pcix_status;
  820. u32 ecc_status;
  821. pci_read_config_word(asd_ha->pcidev, PCI_STATUS, &status);
  822. pci_read_config_dword(asd_ha->pcidev, PCIX_STATUS, &pcix_status);
  823. pci_read_config_dword(asd_ha->pcidev, ECC_CTRL_STAT, &ecc_status);
  824. if (status & PCI_STATUS_DETECTED_PARITY)
  825. asd_printk("parity error for %s\n", pci_name(asd_ha->pcidev));
  826. else if (status & PCI_STATUS_REC_MASTER_ABORT)
  827. asd_printk("master abort for %s\n", pci_name(asd_ha->pcidev));
  828. else if (status & PCI_STATUS_REC_TARGET_ABORT)
  829. asd_printk("target abort for %s\n", pci_name(asd_ha->pcidev));
  830. else if (status & PCI_STATUS_PARITY)
  831. asd_printk("data parity for %s\n", pci_name(asd_ha->pcidev));
  832. else if (pcix_status & RCV_SCE) {
  833. asd_printk("received split completion error for %s\n",
  834. pci_name(asd_ha->pcidev));
  835. pci_write_config_dword(asd_ha->pcidev,PCIX_STATUS,pcix_status);
  836. /* XXX: Abort task? */
  837. return;
  838. } else if (pcix_status & UNEXP_SC) {
  839. asd_printk("unexpected split completion for %s\n",
  840. pci_name(asd_ha->pcidev));
  841. pci_write_config_dword(asd_ha->pcidev,PCIX_STATUS,pcix_status);
  842. /* ignore */
  843. return;
  844. } else if (pcix_status & SC_DISCARD)
  845. asd_printk("split completion discarded for %s\n",
  846. pci_name(asd_ha->pcidev));
  847. else if (ecc_status & UNCOR_ECCERR)
  848. asd_printk("uncorrectable ECC error for %s\n",
  849. pci_name(asd_ha->pcidev));
  850. asd_chip_reset(asd_ha);
  851. }
  852. /**
  853. * asd_hw_isr -- host adapter interrupt service routine
  854. * @irq: ignored
  855. * @dev_id: pointer to host adapter structure
  856. *
  857. * The ISR processes done list entries and level 3 error handling.
  858. */
  859. irqreturn_t asd_hw_isr(int irq, void *dev_id)
  860. {
  861. struct asd_ha_struct *asd_ha = dev_id;
  862. u32 chimint = asd_read_reg_dword(asd_ha, CHIMINT);
  863. if (!chimint)
  864. return IRQ_NONE;
  865. asd_write_reg_dword(asd_ha, CHIMINT, chimint);
  866. (void) asd_read_reg_dword(asd_ha, CHIMINT);
  867. if (chimint & DLAVAIL)
  868. asd_process_donelist_isr(asd_ha);
  869. if (chimint & COMINT)
  870. asd_com_sas_isr(asd_ha);
  871. if (chimint & DEVINT)
  872. asd_dch_sas_isr(asd_ha);
  873. if (chimint & INITERR)
  874. asd_rbi_exsi_isr(asd_ha);
  875. if (chimint & HOSTERR)
  876. asd_hst_pcix_isr(asd_ha);
  877. return IRQ_HANDLED;
  878. }
  879. /* ---------- SCB handling ---------- */
  880. static struct asd_ascb *asd_ascb_alloc(struct asd_ha_struct *asd_ha,
  881. gfp_t gfp_flags)
  882. {
  883. extern struct kmem_cache *asd_ascb_cache;
  884. struct asd_seq_data *seq = &asd_ha->seq;
  885. struct asd_ascb *ascb;
  886. unsigned long flags;
  887. ascb = kmem_cache_zalloc(asd_ascb_cache, gfp_flags);
  888. if (ascb) {
  889. ascb->dma_scb.size = sizeof(struct scb);
  890. ascb->dma_scb.vaddr = dma_pool_zalloc(asd_ha->scb_pool,
  891. gfp_flags,
  892. &ascb->dma_scb.dma_handle);
  893. if (!ascb->dma_scb.vaddr) {
  894. kmem_cache_free(asd_ascb_cache, ascb);
  895. return NULL;
  896. }
  897. asd_init_ascb(asd_ha, ascb);
  898. spin_lock_irqsave(&seq->tc_index_lock, flags);
  899. ascb->tc_index = asd_tc_index_get(seq, ascb);
  900. spin_unlock_irqrestore(&seq->tc_index_lock, flags);
  901. if (ascb->tc_index == -1)
  902. goto undo;
  903. ascb->scb->header.index = cpu_to_le16((u16)ascb->tc_index);
  904. }
  905. return ascb;
  906. undo:
  907. dma_pool_free(asd_ha->scb_pool, ascb->dma_scb.vaddr,
  908. ascb->dma_scb.dma_handle);
  909. kmem_cache_free(asd_ascb_cache, ascb);
  910. ASD_DPRINTK("no index for ascb\n");
  911. return NULL;
  912. }
  913. /**
  914. * asd_ascb_alloc_list -- allocate a list of aSCBs
  915. * @asd_ha: pointer to host adapter structure
  916. * @num: pointer to integer number of aSCBs
  917. * @gfp_flags: GFP_ flags.
  918. *
  919. * This is the only function which is used to allocate aSCBs.
  920. * It can allocate one or many. If more than one, then they form
  921. * a linked list in two ways: by their list field of the ascb struct
  922. * and by the next_scb field of the scb_header.
  923. *
  924. * Returns NULL if no memory was available, else pointer to a list
  925. * of ascbs. When this function returns, @num would be the number
  926. * of SCBs which were not able to be allocated, 0 if all requested
  927. * were able to be allocated.
  928. */
  929. struct asd_ascb *asd_ascb_alloc_list(struct asd_ha_struct
  930. *asd_ha, int *num,
  931. gfp_t gfp_flags)
  932. {
  933. struct asd_ascb *first = NULL;
  934. for ( ; *num > 0; --*num) {
  935. struct asd_ascb *ascb = asd_ascb_alloc(asd_ha, gfp_flags);
  936. if (!ascb)
  937. break;
  938. else if (!first)
  939. first = ascb;
  940. else {
  941. struct asd_ascb *last = list_entry(first->list.prev,
  942. struct asd_ascb,
  943. list);
  944. list_add_tail(&ascb->list, &first->list);
  945. last->scb->header.next_scb =
  946. cpu_to_le64(((u64)ascb->dma_scb.dma_handle));
  947. }
  948. }
  949. return first;
  950. }
  951. /**
  952. * asd_swap_head_scb -- swap the head scb
  953. * @asd_ha: pointer to host adapter structure
  954. * @ascb: pointer to the head of an ascb list
  955. *
  956. * The sequencer knows the DMA address of the next SCB to be DMAed to
  957. * the host adapter, from initialization or from the last list DMAed.
  958. * seq->next_scb keeps the address of this SCB. The sequencer will
  959. * DMA to the host adapter this list of SCBs. But the head (first
  960. * element) of this list is not known to the sequencer. Here we swap
  961. * the head of the list with the known SCB (memcpy()).
  962. * Only one memcpy() is required per list so it is in our interest
  963. * to keep the list of SCB as long as possible so that the ratio
  964. * of number of memcpy calls to the number of SCB DMA-ed is as small
  965. * as possible.
  966. *
  967. * LOCKING: called with the pending list lock held.
  968. */
  969. static void asd_swap_head_scb(struct asd_ha_struct *asd_ha,
  970. struct asd_ascb *ascb)
  971. {
  972. struct asd_seq_data *seq = &asd_ha->seq;
  973. struct asd_ascb *last = list_entry(ascb->list.prev,
  974. struct asd_ascb,
  975. list);
  976. struct asd_dma_tok t = ascb->dma_scb;
  977. memcpy(seq->next_scb.vaddr, ascb->scb, sizeof(*ascb->scb));
  978. ascb->dma_scb = seq->next_scb;
  979. ascb->scb = ascb->dma_scb.vaddr;
  980. seq->next_scb = t;
  981. last->scb->header.next_scb =
  982. cpu_to_le64(((u64)seq->next_scb.dma_handle));
  983. }
  984. /**
  985. * asd_start_scb_timers -- (add and) start timers of SCBs
  986. * @list: pointer to struct list_head of the scbs
  987. *
  988. * If an SCB in the @list has no timer function, assign the default
  989. * one, then start the timer of the SCB. This function is
  990. * intended to be called from asd_post_ascb_list(), just prior to
  991. * posting the SCBs to the sequencer.
  992. */
  993. static void asd_start_scb_timers(struct list_head *list)
  994. {
  995. struct asd_ascb *ascb;
  996. list_for_each_entry(ascb, list, list) {
  997. if (!ascb->uldd_timer) {
  998. ascb->timer.function = asd_ascb_timedout;
  999. ascb->timer.expires = jiffies + AIC94XX_SCB_TIMEOUT;
  1000. add_timer(&ascb->timer);
  1001. }
  1002. }
  1003. }
  1004. /**
  1005. * asd_post_ascb_list -- post a list of 1 or more aSCBs to the host adapter
  1006. * @asd_ha: pointer to a host adapter structure
  1007. * @ascb: pointer to the first aSCB in the list
  1008. * @num: number of aSCBs in the list (to be posted)
  1009. *
  1010. * See queueing comment in asd_post_escb_list().
  1011. *
  1012. * Additional note on queuing: In order to minimize the ratio of memcpy()
  1013. * to the number of ascbs sent, we try to batch-send as many ascbs as possible
  1014. * in one go.
  1015. * Two cases are possible:
  1016. * A) can_queue >= num,
  1017. * B) can_queue < num.
  1018. * Case A: we can send the whole batch at once. Increment "pending"
  1019. * in the beginning of this function, when it is checked, in order to
  1020. * eliminate races when this function is called by multiple processes.
  1021. * Case B: should never happen.
  1022. */
  1023. int asd_post_ascb_list(struct asd_ha_struct *asd_ha, struct asd_ascb *ascb,
  1024. int num)
  1025. {
  1026. unsigned long flags;
  1027. LIST_HEAD(list);
  1028. int can_queue;
  1029. spin_lock_irqsave(&asd_ha->seq.pend_q_lock, flags);
  1030. can_queue = asd_ha->hw_prof.max_scbs - asd_ha->seq.pending;
  1031. if (can_queue >= num)
  1032. asd_ha->seq.pending += num;
  1033. else
  1034. can_queue = 0;
  1035. if (!can_queue) {
  1036. spin_unlock_irqrestore(&asd_ha->seq.pend_q_lock, flags);
  1037. asd_printk("%s: scb queue full\n", pci_name(asd_ha->pcidev));
  1038. return -SAS_QUEUE_FULL;
  1039. }
  1040. asd_swap_head_scb(asd_ha, ascb);
  1041. __list_add(&list, ascb->list.prev, &ascb->list);
  1042. asd_start_scb_timers(&list);
  1043. asd_ha->seq.scbpro += num;
  1044. list_splice_init(&list, asd_ha->seq.pend_q.prev);
  1045. asd_write_reg_dword(asd_ha, SCBPRO, (u32)asd_ha->seq.scbpro);
  1046. spin_unlock_irqrestore(&asd_ha->seq.pend_q_lock, flags);
  1047. return 0;
  1048. }
  1049. /**
  1050. * asd_post_escb_list -- post a list of 1 or more empty scb
  1051. * @asd_ha: pointer to a host adapter structure
  1052. * @ascb: pointer to the first empty SCB in the list
  1053. * @num: number of aSCBs in the list (to be posted)
  1054. *
  1055. * This is essentially the same as asd_post_ascb_list, but we do not
  1056. * increment pending, add those to the pending list or get indexes.
  1057. * See asd_init_escbs() and asd_init_post_escbs().
  1058. *
  1059. * Since sending a list of ascbs is a superset of sending a single
  1060. * ascb, this function exists to generalize this. More specifically,
  1061. * when sending a list of those, we want to do only a _single_
  1062. * memcpy() at swap head, as opposed to for each ascb sent (in the
  1063. * case of sending them one by one). That is, we want to minimize the
  1064. * ratio of memcpy() operations to the number of ascbs sent. The same
  1065. * logic applies to asd_post_ascb_list().
  1066. */
  1067. int asd_post_escb_list(struct asd_ha_struct *asd_ha, struct asd_ascb *ascb,
  1068. int num)
  1069. {
  1070. unsigned long flags;
  1071. spin_lock_irqsave(&asd_ha->seq.pend_q_lock, flags);
  1072. asd_swap_head_scb(asd_ha, ascb);
  1073. asd_ha->seq.scbpro += num;
  1074. asd_write_reg_dword(asd_ha, SCBPRO, (u32)asd_ha->seq.scbpro);
  1075. spin_unlock_irqrestore(&asd_ha->seq.pend_q_lock, flags);
  1076. return 0;
  1077. }
  1078. /* ---------- LED ---------- */
  1079. /**
  1080. * asd_turn_led -- turn on/off an LED
  1081. * @asd_ha: pointer to host adapter structure
  1082. * @phy_id: the PHY id whose LED we want to manupulate
  1083. * @op: 1 to turn on, 0 to turn off
  1084. */
  1085. void asd_turn_led(struct asd_ha_struct *asd_ha, int phy_id, int op)
  1086. {
  1087. if (phy_id < ASD_MAX_PHYS) {
  1088. u32 v = asd_read_reg_dword(asd_ha, LmCONTROL(phy_id));
  1089. if (op)
  1090. v |= LEDPOL;
  1091. else
  1092. v &= ~LEDPOL;
  1093. asd_write_reg_dword(asd_ha, LmCONTROL(phy_id), v);
  1094. }
  1095. }
  1096. /**
  1097. * asd_control_led -- enable/disable an LED on the board
  1098. * @asd_ha: pointer to host adapter structure
  1099. * @phy_id: integer, the phy id
  1100. * @op: integer, 1 to enable, 0 to disable the LED
  1101. *
  1102. * First we output enable the LED, then we set the source
  1103. * to be an external module.
  1104. */
  1105. void asd_control_led(struct asd_ha_struct *asd_ha, int phy_id, int op)
  1106. {
  1107. if (phy_id < ASD_MAX_PHYS) {
  1108. u32 v;
  1109. v = asd_read_reg_dword(asd_ha, GPIOOER);
  1110. if (op)
  1111. v |= (1 << phy_id);
  1112. else
  1113. v &= ~(1 << phy_id);
  1114. asd_write_reg_dword(asd_ha, GPIOOER, v);
  1115. v = asd_read_reg_dword(asd_ha, GPIOCNFGR);
  1116. if (op)
  1117. v |= (1 << phy_id);
  1118. else
  1119. v &= ~(1 << phy_id);
  1120. asd_write_reg_dword(asd_ha, GPIOCNFGR, v);
  1121. }
  1122. }
  1123. /* ---------- PHY enable ---------- */
  1124. static int asd_enable_phy(struct asd_ha_struct *asd_ha, int phy_id)
  1125. {
  1126. struct asd_phy *phy = &asd_ha->phys[phy_id];
  1127. asd_write_reg_byte(asd_ha, LmSEQ_OOB_REG(phy_id, INT_ENABLE_2), 0);
  1128. asd_write_reg_byte(asd_ha, LmSEQ_OOB_REG(phy_id, HOT_PLUG_DELAY),
  1129. HOTPLUG_DELAY_TIMEOUT);
  1130. /* Get defaults from manuf. sector */
  1131. /* XXX we need defaults for those in case MS is broken. */
  1132. asd_write_reg_byte(asd_ha, LmSEQ_OOB_REG(phy_id, PHY_CONTROL_0),
  1133. phy->phy_desc->phy_control_0);
  1134. asd_write_reg_byte(asd_ha, LmSEQ_OOB_REG(phy_id, PHY_CONTROL_1),
  1135. phy->phy_desc->phy_control_1);
  1136. asd_write_reg_byte(asd_ha, LmSEQ_OOB_REG(phy_id, PHY_CONTROL_2),
  1137. phy->phy_desc->phy_control_2);
  1138. asd_write_reg_byte(asd_ha, LmSEQ_OOB_REG(phy_id, PHY_CONTROL_3),
  1139. phy->phy_desc->phy_control_3);
  1140. asd_write_reg_dword(asd_ha, LmSEQ_TEN_MS_COMINIT_TIMEOUT(phy_id),
  1141. ASD_COMINIT_TIMEOUT);
  1142. asd_write_reg_addr(asd_ha, LmSEQ_TX_ID_ADDR_FRAME(phy_id),
  1143. phy->id_frm_tok->dma_handle);
  1144. asd_control_led(asd_ha, phy_id, 1);
  1145. return 0;
  1146. }
  1147. int asd_enable_phys(struct asd_ha_struct *asd_ha, const u8 phy_mask)
  1148. {
  1149. u8 phy_m;
  1150. u8 i;
  1151. int num = 0, k;
  1152. struct asd_ascb *ascb;
  1153. struct asd_ascb *ascb_list;
  1154. if (!phy_mask) {
  1155. asd_printk("%s called with phy_mask of 0!?\n", __func__);
  1156. return 0;
  1157. }
  1158. for_each_phy(phy_mask, phy_m, i) {
  1159. num++;
  1160. asd_enable_phy(asd_ha, i);
  1161. }
  1162. k = num;
  1163. ascb_list = asd_ascb_alloc_list(asd_ha, &k, GFP_KERNEL);
  1164. if (!ascb_list) {
  1165. asd_printk("no memory for control phy ascb list\n");
  1166. return -ENOMEM;
  1167. }
  1168. num -= k;
  1169. ascb = ascb_list;
  1170. for_each_phy(phy_mask, phy_m, i) {
  1171. asd_build_control_phy(ascb, i, ENABLE_PHY);
  1172. ascb = list_entry(ascb->list.next, struct asd_ascb, list);
  1173. }
  1174. ASD_DPRINTK("posting %d control phy scbs\n", num);
  1175. k = asd_post_ascb_list(asd_ha, ascb_list, num);
  1176. if (k)
  1177. asd_ascb_free_list(ascb_list);
  1178. return k;
  1179. }