aic94xx_hwi.c 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392
  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_zalloc(asd_ascb_cache, gfp_flags);
  905. if (ascb) {
  906. ascb->dma_scb.size = sizeof(struct scb);
  907. ascb->dma_scb.vaddr = dma_pool_alloc(asd_ha->scb_pool,
  908. gfp_flags,
  909. &ascb->dma_scb.dma_handle);
  910. if (!ascb->dma_scb.vaddr) {
  911. kmem_cache_free(asd_ascb_cache, ascb);
  912. return NULL;
  913. }
  914. memset(ascb->dma_scb.vaddr, 0, sizeof(struct scb));
  915. asd_init_ascb(asd_ha, ascb);
  916. spin_lock_irqsave(&seq->tc_index_lock, flags);
  917. ascb->tc_index = asd_tc_index_get(seq, ascb);
  918. spin_unlock_irqrestore(&seq->tc_index_lock, flags);
  919. if (ascb->tc_index == -1)
  920. goto undo;
  921. ascb->scb->header.index = cpu_to_le16((u16)ascb->tc_index);
  922. }
  923. return ascb;
  924. undo:
  925. dma_pool_free(asd_ha->scb_pool, ascb->dma_scb.vaddr,
  926. ascb->dma_scb.dma_handle);
  927. kmem_cache_free(asd_ascb_cache, ascb);
  928. ASD_DPRINTK("no index for ascb\n");
  929. return NULL;
  930. }
  931. /**
  932. * asd_ascb_alloc_list -- allocate a list of aSCBs
  933. * @asd_ha: pointer to host adapter structure
  934. * @num: pointer to integer number of aSCBs
  935. * @gfp_flags: GFP_ flags.
  936. *
  937. * This is the only function which is used to allocate aSCBs.
  938. * It can allocate one or many. If more than one, then they form
  939. * a linked list in two ways: by their list field of the ascb struct
  940. * and by the next_scb field of the scb_header.
  941. *
  942. * Returns NULL if no memory was available, else pointer to a list
  943. * of ascbs. When this function returns, @num would be the number
  944. * of SCBs which were not able to be allocated, 0 if all requested
  945. * were able to be allocated.
  946. */
  947. struct asd_ascb *asd_ascb_alloc_list(struct asd_ha_struct
  948. *asd_ha, int *num,
  949. gfp_t gfp_flags)
  950. {
  951. struct asd_ascb *first = NULL;
  952. for ( ; *num > 0; --*num) {
  953. struct asd_ascb *ascb = asd_ascb_alloc(asd_ha, gfp_flags);
  954. if (!ascb)
  955. break;
  956. else if (!first)
  957. first = ascb;
  958. else {
  959. struct asd_ascb *last = list_entry(first->list.prev,
  960. struct asd_ascb,
  961. list);
  962. list_add_tail(&ascb->list, &first->list);
  963. last->scb->header.next_scb =
  964. cpu_to_le64(((u64)ascb->dma_scb.dma_handle));
  965. }
  966. }
  967. return first;
  968. }
  969. /**
  970. * asd_swap_head_scb -- swap the head scb
  971. * @asd_ha: pointer to host adapter structure
  972. * @ascb: pointer to the head of an ascb list
  973. *
  974. * The sequencer knows the DMA address of the next SCB to be DMAed to
  975. * the host adapter, from initialization or from the last list DMAed.
  976. * seq->next_scb keeps the address of this SCB. The sequencer will
  977. * DMA to the host adapter this list of SCBs. But the head (first
  978. * element) of this list is not known to the sequencer. Here we swap
  979. * the head of the list with the known SCB (memcpy()).
  980. * Only one memcpy() is required per list so it is in our interest
  981. * to keep the list of SCB as long as possible so that the ratio
  982. * of number of memcpy calls to the number of SCB DMA-ed is as small
  983. * as possible.
  984. *
  985. * LOCKING: called with the pending list lock held.
  986. */
  987. static inline void asd_swap_head_scb(struct asd_ha_struct *asd_ha,
  988. struct asd_ascb *ascb)
  989. {
  990. struct asd_seq_data *seq = &asd_ha->seq;
  991. struct asd_ascb *last = list_entry(ascb->list.prev,
  992. struct asd_ascb,
  993. list);
  994. struct asd_dma_tok t = ascb->dma_scb;
  995. memcpy(seq->next_scb.vaddr, ascb->scb, sizeof(*ascb->scb));
  996. ascb->dma_scb = seq->next_scb;
  997. ascb->scb = ascb->dma_scb.vaddr;
  998. seq->next_scb = t;
  999. last->scb->header.next_scb =
  1000. cpu_to_le64(((u64)seq->next_scb.dma_handle));
  1001. }
  1002. /**
  1003. * asd_start_timers -- (add and) start timers of SCBs
  1004. * @list: pointer to struct list_head of the scbs
  1005. * @to: timeout in jiffies
  1006. *
  1007. * If an SCB in the @list has no timer function, assign the default
  1008. * one, then start the timer of the SCB. This function is
  1009. * intended to be called from asd_post_ascb_list(), just prior to
  1010. * posting the SCBs to the sequencer.
  1011. */
  1012. static inline void asd_start_scb_timers(struct list_head *list)
  1013. {
  1014. struct asd_ascb *ascb;
  1015. list_for_each_entry(ascb, list, list) {
  1016. if (!ascb->uldd_timer) {
  1017. ascb->timer.data = (unsigned long) ascb;
  1018. ascb->timer.function = asd_ascb_timedout;
  1019. ascb->timer.expires = jiffies + AIC94XX_SCB_TIMEOUT;
  1020. add_timer(&ascb->timer);
  1021. }
  1022. }
  1023. }
  1024. /**
  1025. * asd_post_ascb_list -- post a list of 1 or more aSCBs to the host adapter
  1026. * @asd_ha: pointer to a host adapter structure
  1027. * @ascb: pointer to the first aSCB in the list
  1028. * @num: number of aSCBs in the list (to be posted)
  1029. *
  1030. * See queueing comment in asd_post_escb_list().
  1031. *
  1032. * Additional note on queuing: In order to minimize the ratio of memcpy()
  1033. * to the number of ascbs sent, we try to batch-send as many ascbs as possible
  1034. * in one go.
  1035. * Two cases are possible:
  1036. * A) can_queue >= num,
  1037. * B) can_queue < num.
  1038. * Case A: we can send the whole batch at once. Increment "pending"
  1039. * in the beginning of this function, when it is checked, in order to
  1040. * eliminate races when this function is called by multiple processes.
  1041. * Case B: should never happen if the managing layer considers
  1042. * lldd_queue_size.
  1043. */
  1044. int asd_post_ascb_list(struct asd_ha_struct *asd_ha, struct asd_ascb *ascb,
  1045. int num)
  1046. {
  1047. unsigned long flags;
  1048. LIST_HEAD(list);
  1049. int can_queue;
  1050. spin_lock_irqsave(&asd_ha->seq.pend_q_lock, flags);
  1051. can_queue = asd_ha->hw_prof.max_scbs - asd_ha->seq.pending;
  1052. if (can_queue >= num)
  1053. asd_ha->seq.pending += num;
  1054. else
  1055. can_queue = 0;
  1056. if (!can_queue) {
  1057. spin_unlock_irqrestore(&asd_ha->seq.pend_q_lock, flags);
  1058. asd_printk("%s: scb queue full\n", pci_name(asd_ha->pcidev));
  1059. return -SAS_QUEUE_FULL;
  1060. }
  1061. asd_swap_head_scb(asd_ha, ascb);
  1062. __list_add(&list, ascb->list.prev, &ascb->list);
  1063. asd_start_scb_timers(&list);
  1064. asd_ha->seq.scbpro += num;
  1065. list_splice_init(&list, asd_ha->seq.pend_q.prev);
  1066. asd_write_reg_dword(asd_ha, SCBPRO, (u32)asd_ha->seq.scbpro);
  1067. spin_unlock_irqrestore(&asd_ha->seq.pend_q_lock, flags);
  1068. return 0;
  1069. }
  1070. /**
  1071. * asd_post_escb_list -- post a list of 1 or more empty scb
  1072. * @asd_ha: pointer to a host adapter structure
  1073. * @ascb: pointer to the first empty SCB in the list
  1074. * @num: number of aSCBs in the list (to be posted)
  1075. *
  1076. * This is essentially the same as asd_post_ascb_list, but we do not
  1077. * increment pending, add those to the pending list or get indexes.
  1078. * See asd_init_escbs() and asd_init_post_escbs().
  1079. *
  1080. * Since sending a list of ascbs is a superset of sending a single
  1081. * ascb, this function exists to generalize this. More specifically,
  1082. * when sending a list of those, we want to do only a _single_
  1083. * memcpy() at swap head, as opposed to for each ascb sent (in the
  1084. * case of sending them one by one). That is, we want to minimize the
  1085. * ratio of memcpy() operations to the number of ascbs sent. The same
  1086. * logic applies to asd_post_ascb_list().
  1087. */
  1088. int asd_post_escb_list(struct asd_ha_struct *asd_ha, struct asd_ascb *ascb,
  1089. int num)
  1090. {
  1091. unsigned long flags;
  1092. spin_lock_irqsave(&asd_ha->seq.pend_q_lock, flags);
  1093. asd_swap_head_scb(asd_ha, ascb);
  1094. asd_ha->seq.scbpro += num;
  1095. asd_write_reg_dword(asd_ha, SCBPRO, (u32)asd_ha->seq.scbpro);
  1096. spin_unlock_irqrestore(&asd_ha->seq.pend_q_lock, flags);
  1097. return 0;
  1098. }
  1099. /* ---------- LED ---------- */
  1100. /**
  1101. * asd_turn_led -- turn on/off an LED
  1102. * @asd_ha: pointer to host adapter structure
  1103. * @phy_id: the PHY id whose LED we want to manupulate
  1104. * @op: 1 to turn on, 0 to turn off
  1105. */
  1106. void asd_turn_led(struct asd_ha_struct *asd_ha, int phy_id, int op)
  1107. {
  1108. if (phy_id < ASD_MAX_PHYS) {
  1109. u32 v = asd_read_reg_dword(asd_ha, LmCONTROL(phy_id));
  1110. if (op)
  1111. v |= LEDPOL;
  1112. else
  1113. v &= ~LEDPOL;
  1114. asd_write_reg_dword(asd_ha, LmCONTROL(phy_id), v);
  1115. }
  1116. }
  1117. /**
  1118. * asd_control_led -- enable/disable an LED on the board
  1119. * @asd_ha: pointer to host adapter structure
  1120. * @phy_id: integer, the phy id
  1121. * @op: integer, 1 to enable, 0 to disable the LED
  1122. *
  1123. * First we output enable the LED, then we set the source
  1124. * to be an external module.
  1125. */
  1126. void asd_control_led(struct asd_ha_struct *asd_ha, int phy_id, int op)
  1127. {
  1128. if (phy_id < ASD_MAX_PHYS) {
  1129. u32 v;
  1130. v = asd_read_reg_dword(asd_ha, GPIOOER);
  1131. if (op)
  1132. v |= (1 << phy_id);
  1133. else
  1134. v &= ~(1 << phy_id);
  1135. asd_write_reg_dword(asd_ha, GPIOOER, v);
  1136. v = asd_read_reg_dword(asd_ha, GPIOCNFGR);
  1137. if (op)
  1138. v |= (1 << phy_id);
  1139. else
  1140. v &= ~(1 << phy_id);
  1141. asd_write_reg_dword(asd_ha, GPIOCNFGR, v);
  1142. }
  1143. }
  1144. /* ---------- PHY enable ---------- */
  1145. static int asd_enable_phy(struct asd_ha_struct *asd_ha, int phy_id)
  1146. {
  1147. struct asd_phy *phy = &asd_ha->phys[phy_id];
  1148. asd_write_reg_byte(asd_ha, LmSEQ_OOB_REG(phy_id, INT_ENABLE_2), 0);
  1149. asd_write_reg_byte(asd_ha, LmSEQ_OOB_REG(phy_id, HOT_PLUG_DELAY),
  1150. HOTPLUG_DELAY_TIMEOUT);
  1151. /* Get defaults from manuf. sector */
  1152. /* XXX we need defaults for those in case MS is broken. */
  1153. asd_write_reg_byte(asd_ha, LmSEQ_OOB_REG(phy_id, PHY_CONTROL_0),
  1154. phy->phy_desc->phy_control_0);
  1155. asd_write_reg_byte(asd_ha, LmSEQ_OOB_REG(phy_id, PHY_CONTROL_1),
  1156. phy->phy_desc->phy_control_1);
  1157. asd_write_reg_byte(asd_ha, LmSEQ_OOB_REG(phy_id, PHY_CONTROL_2),
  1158. phy->phy_desc->phy_control_2);
  1159. asd_write_reg_byte(asd_ha, LmSEQ_OOB_REG(phy_id, PHY_CONTROL_3),
  1160. phy->phy_desc->phy_control_3);
  1161. asd_write_reg_dword(asd_ha, LmSEQ_TEN_MS_COMINIT_TIMEOUT(phy_id),
  1162. ASD_COMINIT_TIMEOUT);
  1163. asd_write_reg_addr(asd_ha, LmSEQ_TX_ID_ADDR_FRAME(phy_id),
  1164. phy->id_frm_tok->dma_handle);
  1165. asd_control_led(asd_ha, phy_id, 1);
  1166. return 0;
  1167. }
  1168. int asd_enable_phys(struct asd_ha_struct *asd_ha, const u8 phy_mask)
  1169. {
  1170. u8 phy_m;
  1171. u8 i;
  1172. int num = 0, k;
  1173. struct asd_ascb *ascb;
  1174. struct asd_ascb *ascb_list;
  1175. if (!phy_mask) {
  1176. asd_printk("%s called with phy_mask of 0!?\n", __FUNCTION__);
  1177. return 0;
  1178. }
  1179. for_each_phy(phy_mask, phy_m, i) {
  1180. num++;
  1181. asd_enable_phy(asd_ha, i);
  1182. }
  1183. k = num;
  1184. ascb_list = asd_ascb_alloc_list(asd_ha, &k, GFP_KERNEL);
  1185. if (!ascb_list) {
  1186. asd_printk("no memory for control phy ascb list\n");
  1187. return -ENOMEM;
  1188. }
  1189. num -= k;
  1190. ascb = ascb_list;
  1191. for_each_phy(phy_mask, phy_m, i) {
  1192. asd_build_control_phy(ascb, i, ENABLE_PHY);
  1193. ascb = list_entry(ascb->list.next, struct asd_ascb, list);
  1194. }
  1195. ASD_DPRINTK("posting %d control phy scbs\n", num);
  1196. k = asd_post_ascb_list(asd_ha, ascb_list, num);
  1197. if (k)
  1198. asd_ascb_free_list(ascb_list);
  1199. return k;
  1200. }