aic94xx_hwi.c 38 KB

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