aic94xx_hwi.c 38 KB

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