aic94xx_hwi.c 37 KB

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