aic94xx_hwi.c 38 KB

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