PageRenderTime 47ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/drivers/scsi/aic94xx/aic94xx_dev.c

https://bitbucket.org/abioy/linux
C | 353 lines | 268 code | 44 blank | 41 comment | 59 complexity | a1f52e7033768b815c4f229a83ad6aab MD5 | raw file
Possible License(s): CC-BY-SA-3.0, GPL-2.0, LGPL-2.0, AGPL-1.0
  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 == 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. static int asd_init_sata(struct domain_device *dev)
  99. {
  100. struct asd_ha_struct *asd_ha = dev->port->ha->lldd_ha;
  101. int ddb = (int) (unsigned long) dev->lldd_dev;
  102. u32 qdepth = 0;
  103. int res = 0;
  104. asd_ddbsite_write_word(asd_ha, ddb, ATA_CMD_SCBPTR, 0xFFFF);
  105. if ((dev->dev_type == SATA_DEV || dev->dev_type == SATA_PM_PORT) &&
  106. dev->sata_dev.identify_device &&
  107. dev->sata_dev.identify_device[10] != 0) {
  108. u16 w75 = le16_to_cpu(dev->sata_dev.identify_device[75]);
  109. u16 w76 = le16_to_cpu(dev->sata_dev.identify_device[76]);
  110. if (w76 & 0x100) /* NCQ? */
  111. qdepth = (w75 & 0x1F) + 1;
  112. asd_ddbsite_write_dword(asd_ha, ddb, SATA_TAG_ALLOC_MASK,
  113. (1ULL<<qdepth)-1);
  114. asd_ddbsite_write_byte(asd_ha, ddb, NUM_SATA_TAGS, qdepth);
  115. }
  116. if (dev->dev_type == SATA_DEV || dev->dev_type == SATA_PM ||
  117. dev->dev_type == SATA_PM_PORT) {
  118. struct dev_to_host_fis *fis = (struct dev_to_host_fis *)
  119. dev->frame_rcvd;
  120. asd_ddbsite_write_byte(asd_ha, ddb, SATA_STATUS, fis->status);
  121. }
  122. asd_ddbsite_write_word(asd_ha, ddb, NCQ_DATA_SCB_PTR, 0xFFFF);
  123. if (qdepth > 0)
  124. res = asd_init_sata_tag_ddb(dev);
  125. return res;
  126. }
  127. static int asd_init_target_ddb(struct domain_device *dev)
  128. {
  129. int ddb, i;
  130. struct asd_ha_struct *asd_ha = dev->port->ha->lldd_ha;
  131. u8 flags = 0;
  132. ddb = asd_get_ddb(asd_ha);
  133. if (ddb < 0)
  134. return ddb;
  135. dev->lldd_dev = (void *) (unsigned long) ddb;
  136. asd_ddbsite_write_byte(asd_ha, ddb, 0, DDB_TP_CONN_TYPE);
  137. asd_ddbsite_write_byte(asd_ha, ddb, 1, 0);
  138. asd_ddbsite_write_word(asd_ha, ddb, INIT_CONN_TAG, 0xFFFF);
  139. for (i = 0; i < SAS_ADDR_SIZE; i++)
  140. asd_ddbsite_write_byte(asd_ha, ddb, DEST_SAS_ADDR+i,
  141. dev->sas_addr[i]);
  142. asd_ddbsite_write_word(asd_ha, ddb, SEND_QUEUE_HEAD, 0xFFFF);
  143. asd_set_ddb_type(dev);
  144. asd_ddbsite_write_byte(asd_ha, ddb, CONN_MASK, dev->port->phy_mask);
  145. if (dev->port->oob_mode != SATA_OOB_MODE) {
  146. flags |= OPEN_REQUIRED;
  147. if ((dev->dev_type == SATA_DEV) ||
  148. (dev->tproto & SAS_PROTOCOL_STP)) {
  149. struct smp_resp *rps_resp = &dev->sata_dev.rps_resp;
  150. if (rps_resp->frame_type == SMP_RESPONSE &&
  151. rps_resp->function == SMP_REPORT_PHY_SATA &&
  152. rps_resp->result == SMP_RESP_FUNC_ACC) {
  153. if (rps_resp->rps.affil_valid)
  154. flags |= STP_AFFIL_POL;
  155. if (rps_resp->rps.affil_supp)
  156. flags |= SUPPORTS_AFFIL;
  157. }
  158. } else {
  159. flags |= CONCURRENT_CONN_SUPP;
  160. if (!dev->parent &&
  161. (dev->dev_type == EDGE_DEV ||
  162. dev->dev_type == FANOUT_DEV))
  163. asd_ddbsite_write_byte(asd_ha, ddb, MAX_CCONN,
  164. 4);
  165. else
  166. asd_ddbsite_write_byte(asd_ha, ddb, MAX_CCONN,
  167. dev->pathways);
  168. asd_ddbsite_write_byte(asd_ha, ddb, NUM_CTX, 1);
  169. }
  170. }
  171. if (dev->dev_type == SATA_PM)
  172. flags |= SATA_MULTIPORT;
  173. asd_ddbsite_write_byte(asd_ha, ddb, DDB_TARG_FLAGS, flags);
  174. flags = 0;
  175. if (dev->tproto & SAS_PROTOCOL_STP)
  176. flags |= STP_CL_POL_NO_TX;
  177. asd_ddbsite_write_byte(asd_ha, ddb, DDB_TARG_FLAGS2, flags);
  178. asd_ddbsite_write_word(asd_ha, ddb, EXEC_QUEUE_TAIL, 0xFFFF);
  179. asd_ddbsite_write_word(asd_ha, ddb, SEND_QUEUE_TAIL, 0xFFFF);
  180. asd_ddbsite_write_word(asd_ha, ddb, SISTER_DDB, 0xFFFF);
  181. if (dev->dev_type == SATA_DEV || (dev->tproto & SAS_PROTOCOL_STP)) {
  182. i = asd_init_sata(dev);
  183. if (i < 0) {
  184. asd_free_ddb(asd_ha, ddb);
  185. return i;
  186. }
  187. }
  188. if (dev->dev_type == SAS_END_DEV) {
  189. struct sas_end_device *rdev = rphy_to_end_device(dev->rphy);
  190. if (rdev->I_T_nexus_loss_timeout > 0)
  191. asd_ddbsite_write_word(asd_ha, ddb, ITNL_TIMEOUT,
  192. min(rdev->I_T_nexus_loss_timeout,
  193. (u16)ITNL_TIMEOUT_CONST));
  194. else
  195. asd_ddbsite_write_word(asd_ha, ddb, ITNL_TIMEOUT,
  196. (u16)ITNL_TIMEOUT_CONST);
  197. }
  198. return 0;
  199. }
  200. static int asd_init_sata_pm_table_ddb(struct domain_device *dev)
  201. {
  202. struct asd_ha_struct *asd_ha = dev->port->ha->lldd_ha;
  203. int ddb, i;
  204. ddb = asd_get_ddb(asd_ha);
  205. if (ddb < 0)
  206. return ddb;
  207. for (i = 0; i < 32; i += 2)
  208. asd_ddbsite_write_word(asd_ha, ddb, i, 0xFFFF);
  209. asd_ddbsite_write_word(asd_ha, (int) (unsigned long) dev->lldd_dev,
  210. SISTER_DDB, ddb);
  211. return 0;
  212. }
  213. #define PM_PORT_FLAGS offsetof(struct asd_ddb_sata_pm_port, pm_port_flags)
  214. #define PARENT_DDB offsetof(struct asd_ddb_sata_pm_port, parent_ddb)
  215. /**
  216. * asd_init_sata_pm_port_ddb -- SATA Port Multiplier Port
  217. * dev: pointer to domain device
  218. *
  219. * For SATA Port Multiplier Ports we need to allocate one SATA Port
  220. * Multiplier Port DDB and depending on whether the target on it
  221. * supports SATA II NCQ, one SATA Tag DDB.
  222. */
  223. static int asd_init_sata_pm_port_ddb(struct domain_device *dev)
  224. {
  225. int ddb, i, parent_ddb, pmtable_ddb;
  226. struct asd_ha_struct *asd_ha = dev->port->ha->lldd_ha;
  227. u8 flags;
  228. ddb = asd_get_ddb(asd_ha);
  229. if (ddb < 0)
  230. return ddb;
  231. asd_set_ddb_type(dev);
  232. flags = (dev->sata_dev.port_no << 4) | PM_PORT_SET;
  233. asd_ddbsite_write_byte(asd_ha, ddb, PM_PORT_FLAGS, flags);
  234. asd_ddbsite_write_word(asd_ha, ddb, SISTER_DDB, 0xFFFF);
  235. asd_ddbsite_write_word(asd_ha, ddb, ATA_CMD_SCBPTR, 0xFFFF);
  236. asd_init_sata(dev);
  237. parent_ddb = (int) (unsigned long) dev->parent->lldd_dev;
  238. asd_ddbsite_write_word(asd_ha, ddb, PARENT_DDB, parent_ddb);
  239. pmtable_ddb = asd_ddbsite_read_word(asd_ha, parent_ddb, SISTER_DDB);
  240. asd_ddbsite_write_word(asd_ha, pmtable_ddb, dev->sata_dev.port_no,ddb);
  241. if (asd_ddbsite_read_byte(asd_ha, ddb, NUM_SATA_TAGS) > 0) {
  242. i = asd_init_sata_tag_ddb(dev);
  243. if (i < 0) {
  244. asd_free_ddb(asd_ha, ddb);
  245. return i;
  246. }
  247. }
  248. return 0;
  249. }
  250. static int asd_init_initiator_ddb(struct domain_device *dev)
  251. {
  252. return -ENODEV;
  253. }
  254. /**
  255. * asd_init_sata_pm_ddb -- SATA Port Multiplier
  256. * dev: pointer to domain device
  257. *
  258. * For STP and direct-attached SATA Port Multipliers we need
  259. * one target port DDB entry and one SATA PM table DDB entry.
  260. */
  261. static int asd_init_sata_pm_ddb(struct domain_device *dev)
  262. {
  263. int res = 0;
  264. res = asd_init_target_ddb(dev);
  265. if (res)
  266. goto out;
  267. res = asd_init_sata_pm_table_ddb(dev);
  268. if (res)
  269. asd_free_ddb(dev->port->ha->lldd_ha,
  270. (int) (unsigned long) dev->lldd_dev);
  271. out:
  272. return res;
  273. }
  274. int asd_dev_found(struct domain_device *dev)
  275. {
  276. unsigned long flags;
  277. int res = 0;
  278. struct asd_ha_struct *asd_ha = dev->port->ha->lldd_ha;
  279. spin_lock_irqsave(&asd_ha->hw_prof.ddb_lock, flags);
  280. switch (dev->dev_type) {
  281. case SATA_PM:
  282. res = asd_init_sata_pm_ddb(dev);
  283. break;
  284. case SATA_PM_PORT:
  285. res = asd_init_sata_pm_port_ddb(dev);
  286. break;
  287. default:
  288. if (dev->tproto)
  289. res = asd_init_target_ddb(dev);
  290. else
  291. res = asd_init_initiator_ddb(dev);
  292. }
  293. spin_unlock_irqrestore(&asd_ha->hw_prof.ddb_lock, flags);
  294. return res;
  295. }
  296. void asd_dev_gone(struct domain_device *dev)
  297. {
  298. int ddb, sister_ddb;
  299. unsigned long flags;
  300. struct asd_ha_struct *asd_ha = dev->port->ha->lldd_ha;
  301. spin_lock_irqsave(&asd_ha->hw_prof.ddb_lock, flags);
  302. ddb = (int) (unsigned long) dev->lldd_dev;
  303. sister_ddb = asd_ddbsite_read_word(asd_ha, ddb, SISTER_DDB);
  304. if (sister_ddb != 0xFFFF)
  305. asd_free_ddb(asd_ha, sister_ddb);
  306. asd_free_ddb(asd_ha, ddb);
  307. dev->lldd_dev = NULL;
  308. spin_unlock_irqrestore(&asd_ha->hw_prof.ddb_lock, flags);
  309. }