aic94xx_dev.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  1. /*
  2. * Aic94xx SAS/SATA DDB management
  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. * $Id: //depot/aic94xx/aic94xx_dev.c#21 $
  26. */
  27. #include "aic94xx.h"
  28. #include "aic94xx_hwi.h"
  29. #include "aic94xx_reg.h"
  30. #include "aic94xx_sas.h"
  31. #define FIND_FREE_DDB(_ha) find_first_zero_bit((_ha)->hw_prof.ddb_bitmap, \
  32. (_ha)->hw_prof.max_ddbs)
  33. #define SET_DDB(_ddb, _ha) set_bit(_ddb, (_ha)->hw_prof.ddb_bitmap)
  34. #define CLEAR_DDB(_ddb, _ha) clear_bit(_ddb, (_ha)->hw_prof.ddb_bitmap)
  35. static int asd_get_ddb(struct asd_ha_struct *asd_ha)
  36. {
  37. int ddb, i;
  38. ddb = FIND_FREE_DDB(asd_ha);
  39. if (ddb >= asd_ha->hw_prof.max_ddbs) {
  40. ddb = -ENOMEM;
  41. goto out;
  42. }
  43. SET_DDB(ddb, asd_ha);
  44. for (i = 0; i < sizeof(struct asd_ddb_ssp_smp_target_port); i+= 4)
  45. asd_ddbsite_write_dword(asd_ha, ddb, i, 0);
  46. out:
  47. return ddb;
  48. }
  49. #define INIT_CONN_TAG offsetof(struct asd_ddb_ssp_smp_target_port, init_conn_tag)
  50. #define DEST_SAS_ADDR offsetof(struct asd_ddb_ssp_smp_target_port, dest_sas_addr)
  51. #define SEND_QUEUE_HEAD offsetof(struct asd_ddb_ssp_smp_target_port, send_queue_head)
  52. #define DDB_TYPE offsetof(struct asd_ddb_ssp_smp_target_port, ddb_type)
  53. #define CONN_MASK offsetof(struct asd_ddb_ssp_smp_target_port, conn_mask)
  54. #define DDB_TARG_FLAGS offsetof(struct asd_ddb_ssp_smp_target_port, flags)
  55. #define DDB_TARG_FLAGS2 offsetof(struct asd_ddb_stp_sata_target_port, flags2)
  56. #define EXEC_QUEUE_TAIL offsetof(struct asd_ddb_ssp_smp_target_port, exec_queue_tail)
  57. #define SEND_QUEUE_TAIL offsetof(struct asd_ddb_ssp_smp_target_port, send_queue_tail)
  58. #define SISTER_DDB offsetof(struct asd_ddb_ssp_smp_target_port, sister_ddb)
  59. #define MAX_CCONN offsetof(struct asd_ddb_ssp_smp_target_port, max_concurrent_conn)
  60. #define NUM_CTX offsetof(struct asd_ddb_ssp_smp_target_port, num_contexts)
  61. #define ATA_CMD_SCBPTR offsetof(struct asd_ddb_stp_sata_target_port, ata_cmd_scbptr)
  62. #define SATA_TAG_ALLOC_MASK offsetof(struct asd_ddb_stp_sata_target_port, sata_tag_alloc_mask)
  63. #define NUM_SATA_TAGS offsetof(struct asd_ddb_stp_sata_target_port, num_sata_tags)
  64. #define SATA_STATUS offsetof(struct asd_ddb_stp_sata_target_port, sata_status)
  65. #define NCQ_DATA_SCB_PTR offsetof(struct asd_ddb_stp_sata_target_port, ncq_data_scb_ptr)
  66. #define ITNL_TIMEOUT offsetof(struct asd_ddb_ssp_smp_target_port, itnl_timeout)
  67. static void asd_free_ddb(struct asd_ha_struct *asd_ha, int ddb)
  68. {
  69. if (!ddb || ddb >= 0xFFFF)
  70. return;
  71. asd_ddbsite_write_byte(asd_ha, ddb, DDB_TYPE, DDB_TYPE_UNUSED);
  72. CLEAR_DDB(ddb, asd_ha);
  73. }
  74. static void asd_set_ddb_type(struct domain_device *dev)
  75. {
  76. struct asd_ha_struct *asd_ha = dev->port->ha->lldd_ha;
  77. int ddb = (int) (unsigned long) dev->lldd_dev;
  78. if (dev->dev_type == SAS_SATA_PM_PORT)
  79. asd_ddbsite_write_byte(asd_ha,ddb, DDB_TYPE, DDB_TYPE_PM_PORT);
  80. else if (dev->tproto)
  81. asd_ddbsite_write_byte(asd_ha,ddb, DDB_TYPE, DDB_TYPE_TARGET);
  82. else
  83. asd_ddbsite_write_byte(asd_ha,ddb,DDB_TYPE,DDB_TYPE_INITIATOR);
  84. }
  85. static int asd_init_sata_tag_ddb(struct domain_device *dev)
  86. {
  87. struct asd_ha_struct *asd_ha = dev->port->ha->lldd_ha;
  88. int ddb, i;
  89. ddb = asd_get_ddb(asd_ha);
  90. if (ddb < 0)
  91. return ddb;
  92. for (i = 0; i < sizeof(struct asd_ddb_sata_tag); i += 2)
  93. asd_ddbsite_write_word(asd_ha, ddb, i, 0xFFFF);
  94. asd_ddbsite_write_word(asd_ha, (int) (unsigned long) dev->lldd_dev,
  95. SISTER_DDB, ddb);
  96. return 0;
  97. }
  98. void asd_set_dmamode(struct domain_device *dev)
  99. {
  100. struct asd_ha_struct *asd_ha = dev->port->ha->lldd_ha;
  101. struct ata_device *ata_dev = sas_to_ata_dev(dev);
  102. int ddb = (int) (unsigned long) dev->lldd_dev;
  103. u32 qdepth = 0;
  104. if (dev->dev_type == SAS_SATA_DEV || dev->dev_type == SAS_SATA_PM_PORT) {
  105. if (ata_id_has_ncq(ata_dev->id))
  106. qdepth = ata_id_queue_depth(ata_dev->id);
  107. asd_ddbsite_write_dword(asd_ha, ddb, SATA_TAG_ALLOC_MASK,
  108. (1ULL<<qdepth)-1);
  109. asd_ddbsite_write_byte(asd_ha, ddb, NUM_SATA_TAGS, qdepth);
  110. }
  111. if (qdepth > 0)
  112. if (asd_init_sata_tag_ddb(dev) != 0) {
  113. unsigned long flags;
  114. spin_lock_irqsave(dev->sata_dev.ap->lock, flags);
  115. ata_dev->flags |= ATA_DFLAG_NCQ_OFF;
  116. spin_unlock_irqrestore(dev->sata_dev.ap->lock, flags);
  117. }
  118. }
  119. static int asd_init_sata(struct domain_device *dev)
  120. {
  121. struct asd_ha_struct *asd_ha = dev->port->ha->lldd_ha;
  122. int ddb = (int) (unsigned long) dev->lldd_dev;
  123. asd_ddbsite_write_word(asd_ha, ddb, ATA_CMD_SCBPTR, 0xFFFF);
  124. if (dev->dev_type == SAS_SATA_DEV || dev->dev_type == SAS_SATA_PM ||
  125. dev->dev_type == SAS_SATA_PM_PORT) {
  126. struct dev_to_host_fis *fis = (struct dev_to_host_fis *)
  127. dev->frame_rcvd;
  128. asd_ddbsite_write_byte(asd_ha, ddb, SATA_STATUS, fis->status);
  129. }
  130. asd_ddbsite_write_word(asd_ha, ddb, NCQ_DATA_SCB_PTR, 0xFFFF);
  131. return 0;
  132. }
  133. static int asd_init_target_ddb(struct domain_device *dev)
  134. {
  135. int ddb, i;
  136. struct asd_ha_struct *asd_ha = dev->port->ha->lldd_ha;
  137. u8 flags = 0;
  138. ddb = asd_get_ddb(asd_ha);
  139. if (ddb < 0)
  140. return ddb;
  141. dev->lldd_dev = (void *) (unsigned long) ddb;
  142. asd_ddbsite_write_byte(asd_ha, ddb, 0, DDB_TP_CONN_TYPE);
  143. asd_ddbsite_write_byte(asd_ha, ddb, 1, 0);
  144. asd_ddbsite_write_word(asd_ha, ddb, INIT_CONN_TAG, 0xFFFF);
  145. for (i = 0; i < SAS_ADDR_SIZE; i++)
  146. asd_ddbsite_write_byte(asd_ha, ddb, DEST_SAS_ADDR+i,
  147. dev->sas_addr[i]);
  148. asd_ddbsite_write_word(asd_ha, ddb, SEND_QUEUE_HEAD, 0xFFFF);
  149. asd_set_ddb_type(dev);
  150. asd_ddbsite_write_byte(asd_ha, ddb, CONN_MASK, dev->port->phy_mask);
  151. if (dev->port->oob_mode != SATA_OOB_MODE) {
  152. flags |= OPEN_REQUIRED;
  153. if ((dev->dev_type == SAS_SATA_DEV) ||
  154. (dev->tproto & SAS_PROTOCOL_STP)) {
  155. struct smp_resp *rps_resp = &dev->sata_dev.rps_resp;
  156. if (rps_resp->frame_type == SMP_RESPONSE &&
  157. rps_resp->function == SMP_REPORT_PHY_SATA &&
  158. rps_resp->result == SMP_RESP_FUNC_ACC) {
  159. if (rps_resp->rps.affil_valid)
  160. flags |= STP_AFFIL_POL;
  161. if (rps_resp->rps.affil_supp)
  162. flags |= SUPPORTS_AFFIL;
  163. }
  164. } else {
  165. flags |= CONCURRENT_CONN_SUPP;
  166. if (!dev->parent &&
  167. (dev->dev_type == SAS_EDGE_EXPANDER_DEVICE ||
  168. dev->dev_type == SAS_FANOUT_EXPANDER_DEVICE))
  169. asd_ddbsite_write_byte(asd_ha, ddb, MAX_CCONN,
  170. 4);
  171. else
  172. asd_ddbsite_write_byte(asd_ha, ddb, MAX_CCONN,
  173. dev->pathways);
  174. asd_ddbsite_write_byte(asd_ha, ddb, NUM_CTX, 1);
  175. }
  176. }
  177. if (dev->dev_type == SAS_SATA_PM)
  178. flags |= SATA_MULTIPORT;
  179. asd_ddbsite_write_byte(asd_ha, ddb, DDB_TARG_FLAGS, flags);
  180. flags = 0;
  181. if (dev->tproto & SAS_PROTOCOL_STP)
  182. flags |= STP_CL_POL_NO_TX;
  183. asd_ddbsite_write_byte(asd_ha, ddb, DDB_TARG_FLAGS2, flags);
  184. asd_ddbsite_write_word(asd_ha, ddb, EXEC_QUEUE_TAIL, 0xFFFF);
  185. asd_ddbsite_write_word(asd_ha, ddb, SEND_QUEUE_TAIL, 0xFFFF);
  186. asd_ddbsite_write_word(asd_ha, ddb, SISTER_DDB, 0xFFFF);
  187. if (dev->dev_type == SAS_SATA_DEV || (dev->tproto & SAS_PROTOCOL_STP)) {
  188. i = asd_init_sata(dev);
  189. if (i < 0) {
  190. asd_free_ddb(asd_ha, ddb);
  191. return i;
  192. }
  193. }
  194. if (dev->dev_type == SAS_END_DEVICE) {
  195. struct sas_end_device *rdev = rphy_to_end_device(dev->rphy);
  196. if (rdev->I_T_nexus_loss_timeout > 0)
  197. asd_ddbsite_write_word(asd_ha, ddb, ITNL_TIMEOUT,
  198. min(rdev->I_T_nexus_loss_timeout,
  199. (u16)ITNL_TIMEOUT_CONST));
  200. else
  201. asd_ddbsite_write_word(asd_ha, ddb, ITNL_TIMEOUT,
  202. (u16)ITNL_TIMEOUT_CONST);
  203. }
  204. return 0;
  205. }
  206. static int asd_init_sata_pm_table_ddb(struct domain_device *dev)
  207. {
  208. struct asd_ha_struct *asd_ha = dev->port->ha->lldd_ha;
  209. int ddb, i;
  210. ddb = asd_get_ddb(asd_ha);
  211. if (ddb < 0)
  212. return ddb;
  213. for (i = 0; i < 32; i += 2)
  214. asd_ddbsite_write_word(asd_ha, ddb, i, 0xFFFF);
  215. asd_ddbsite_write_word(asd_ha, (int) (unsigned long) dev->lldd_dev,
  216. SISTER_DDB, ddb);
  217. return 0;
  218. }
  219. #define PM_PORT_FLAGS offsetof(struct asd_ddb_sata_pm_port, pm_port_flags)
  220. #define PARENT_DDB offsetof(struct asd_ddb_sata_pm_port, parent_ddb)
  221. /**
  222. * asd_init_sata_pm_port_ddb -- SATA Port Multiplier Port
  223. * dev: pointer to domain device
  224. *
  225. * For SATA Port Multiplier Ports we need to allocate one SATA Port
  226. * Multiplier Port DDB and depending on whether the target on it
  227. * supports SATA II NCQ, one SATA Tag DDB.
  228. */
  229. static int asd_init_sata_pm_port_ddb(struct domain_device *dev)
  230. {
  231. int ddb, i, parent_ddb, pmtable_ddb;
  232. struct asd_ha_struct *asd_ha = dev->port->ha->lldd_ha;
  233. u8 flags;
  234. ddb = asd_get_ddb(asd_ha);
  235. if (ddb < 0)
  236. return ddb;
  237. asd_set_ddb_type(dev);
  238. flags = (dev->sata_dev.port_no << 4) | PM_PORT_SET;
  239. asd_ddbsite_write_byte(asd_ha, ddb, PM_PORT_FLAGS, flags);
  240. asd_ddbsite_write_word(asd_ha, ddb, SISTER_DDB, 0xFFFF);
  241. asd_ddbsite_write_word(asd_ha, ddb, ATA_CMD_SCBPTR, 0xFFFF);
  242. asd_init_sata(dev);
  243. parent_ddb = (int) (unsigned long) dev->parent->lldd_dev;
  244. asd_ddbsite_write_word(asd_ha, ddb, PARENT_DDB, parent_ddb);
  245. pmtable_ddb = asd_ddbsite_read_word(asd_ha, parent_ddb, SISTER_DDB);
  246. asd_ddbsite_write_word(asd_ha, pmtable_ddb, dev->sata_dev.port_no,ddb);
  247. if (asd_ddbsite_read_byte(asd_ha, ddb, NUM_SATA_TAGS) > 0) {
  248. i = asd_init_sata_tag_ddb(dev);
  249. if (i < 0) {
  250. asd_free_ddb(asd_ha, ddb);
  251. return i;
  252. }
  253. }
  254. return 0;
  255. }
  256. static int asd_init_initiator_ddb(struct domain_device *dev)
  257. {
  258. return -ENODEV;
  259. }
  260. /**
  261. * asd_init_sata_pm_ddb -- SATA Port Multiplier
  262. * dev: pointer to domain device
  263. *
  264. * For STP and direct-attached SATA Port Multipliers we need
  265. * one target port DDB entry and one SATA PM table DDB entry.
  266. */
  267. static int asd_init_sata_pm_ddb(struct domain_device *dev)
  268. {
  269. int res = 0;
  270. res = asd_init_target_ddb(dev);
  271. if (res)
  272. goto out;
  273. res = asd_init_sata_pm_table_ddb(dev);
  274. if (res)
  275. asd_free_ddb(dev->port->ha->lldd_ha,
  276. (int) (unsigned long) dev->lldd_dev);
  277. out:
  278. return res;
  279. }
  280. int asd_dev_found(struct domain_device *dev)
  281. {
  282. unsigned long flags;
  283. int res = 0;
  284. struct asd_ha_struct *asd_ha = dev->port->ha->lldd_ha;
  285. spin_lock_irqsave(&asd_ha->hw_prof.ddb_lock, flags);
  286. switch (dev->dev_type) {
  287. case SAS_SATA_PM:
  288. res = asd_init_sata_pm_ddb(dev);
  289. break;
  290. case SAS_SATA_PM_PORT:
  291. res = asd_init_sata_pm_port_ddb(dev);
  292. break;
  293. default:
  294. if (dev->tproto)
  295. res = asd_init_target_ddb(dev);
  296. else
  297. res = asd_init_initiator_ddb(dev);
  298. }
  299. spin_unlock_irqrestore(&asd_ha->hw_prof.ddb_lock, flags);
  300. return res;
  301. }
  302. void asd_dev_gone(struct domain_device *dev)
  303. {
  304. int ddb, sister_ddb;
  305. unsigned long flags;
  306. struct asd_ha_struct *asd_ha = dev->port->ha->lldd_ha;
  307. spin_lock_irqsave(&asd_ha->hw_prof.ddb_lock, flags);
  308. ddb = (int) (unsigned long) dev->lldd_dev;
  309. sister_ddb = asd_ddbsite_read_word(asd_ha, ddb, SISTER_DDB);
  310. if (sister_ddb != 0xFFFF)
  311. asd_free_ddb(asd_ha, sister_ddb);
  312. asd_free_ddb(asd_ha, ddb);
  313. dev->lldd_dev = NULL;
  314. spin_unlock_irqrestore(&asd_ha->hw_prof.ddb_lock, flags);
  315. }