aic94xx_hwi.c 38 KB

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