esas2r_targdb.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. /*
  2. * linux/drivers/scsi/esas2r/esas2r_targdb.c
  3. * For use with ATTO ExpressSAS R6xx SAS/SATA RAID controllers
  4. *
  5. * Copyright (c) 2001-2013 ATTO Technology, Inc.
  6. * (mailto:linuxdrivers@attotech.com)
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License
  10. * as published by the Free Software Foundation; either version 2
  11. * of the License, or (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * NO WARRANTY
  19. * THE PROGRAM IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OR
  20. * CONDITIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED INCLUDING, WITHOUT
  21. * LIMITATION, ANY WARRANTIES OR CONDITIONS OF TITLE, NON-INFRINGEMENT,
  22. * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. Each Recipient is
  23. * solely responsible for determining the appropriateness of using and
  24. * distributing the Program and assumes all risks associated with its
  25. * exercise of rights under this Agreement, including but not limited to
  26. * the risks and costs of program errors, damage to or loss of data,
  27. * programs or equipment, and unavailability or interruption of operations.
  28. *
  29. * DISCLAIMER OF LIABILITY
  30. * NEITHER RECIPIENT NOR ANY CONTRIBUTORS SHALL HAVE ANY LIABILITY FOR ANY
  31. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  32. * DAMAGES (INCLUDING WITHOUT LIMITATION LOST PROFITS), HOWEVER CAUSED AND
  33. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
  34. * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
  35. * USE OR DISTRIBUTION OF THE PROGRAM OR THE EXERCISE OF ANY RIGHTS GRANTED
  36. * HEREUNDER, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGES
  37. *
  38. * You should have received a copy of the GNU General Public License
  39. * along with this program; if not, write to the Free Software
  40. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
  41. * USA.
  42. */
  43. #include "esas2r.h"
  44. void esas2r_targ_db_initialize(struct esas2r_adapter *a)
  45. {
  46. struct esas2r_target *t;
  47. for (t = a->targetdb; t < a->targetdb_end; t++) {
  48. memset(t, 0, sizeof(struct esas2r_target));
  49. t->target_state = TS_NOT_PRESENT;
  50. t->buffered_target_state = TS_NOT_PRESENT;
  51. t->new_target_state = TS_INVALID;
  52. }
  53. }
  54. void esas2r_targ_db_remove_all(struct esas2r_adapter *a, bool notify)
  55. {
  56. struct esas2r_target *t;
  57. unsigned long flags;
  58. for (t = a->targetdb; t < a->targetdb_end; t++) {
  59. if (t->target_state != TS_PRESENT)
  60. continue;
  61. spin_lock_irqsave(&a->mem_lock, flags);
  62. esas2r_targ_db_remove(a, t);
  63. spin_unlock_irqrestore(&a->mem_lock, flags);
  64. if (notify) {
  65. esas2r_trace("remove id:%d", esas2r_targ_get_id(t,
  66. a));
  67. esas2r_target_state_changed(a, esas2r_targ_get_id(t,
  68. a),
  69. TS_NOT_PRESENT);
  70. }
  71. }
  72. }
  73. void esas2r_targ_db_report_changes(struct esas2r_adapter *a)
  74. {
  75. struct esas2r_target *t;
  76. unsigned long flags;
  77. esas2r_trace_enter();
  78. if (test_bit(AF_DISC_PENDING, &a->flags)) {
  79. esas2r_trace_exit();
  80. return;
  81. }
  82. for (t = a->targetdb; t < a->targetdb_end; t++) {
  83. u8 state = TS_INVALID;
  84. spin_lock_irqsave(&a->mem_lock, flags);
  85. if (t->buffered_target_state != t->target_state)
  86. state = t->buffered_target_state = t->target_state;
  87. spin_unlock_irqrestore(&a->mem_lock, flags);
  88. if (state != TS_INVALID) {
  89. esas2r_trace("targ_db_report_changes:%d",
  90. esas2r_targ_get_id(
  91. t,
  92. a));
  93. esas2r_trace("state:%d", state);
  94. esas2r_target_state_changed(a,
  95. esas2r_targ_get_id(t,
  96. a),
  97. state);
  98. }
  99. }
  100. esas2r_trace_exit();
  101. }
  102. struct esas2r_target *esas2r_targ_db_add_raid(struct esas2r_adapter *a,
  103. struct esas2r_disc_context *
  104. dc)
  105. {
  106. struct esas2r_target *t;
  107. esas2r_trace_enter();
  108. if (dc->curr_virt_id >= ESAS2R_MAX_TARGETS) {
  109. esas2r_bugon();
  110. esas2r_trace_exit();
  111. return NULL;
  112. }
  113. t = a->targetdb + dc->curr_virt_id;
  114. if (t->target_state == TS_PRESENT) {
  115. esas2r_trace_exit();
  116. return NULL;
  117. }
  118. esas2r_hdebug("add RAID %s, T:%d", dc->raid_grp_name,
  119. esas2r_targ_get_id(
  120. t,
  121. a));
  122. if (dc->interleave == 0
  123. || dc->block_size == 0) {
  124. /* these are invalid values, don't create the target entry. */
  125. esas2r_hdebug("invalid RAID group dimensions");
  126. esas2r_trace_exit();
  127. return NULL;
  128. }
  129. t->block_size = dc->block_size;
  130. t->inter_byte = dc->interleave;
  131. t->inter_block = dc->interleave / dc->block_size;
  132. t->virt_targ_id = dc->curr_virt_id;
  133. t->phys_targ_id = ESAS2R_TARG_ID_INV;
  134. t->flags &= ~TF_PASS_THRU;
  135. t->flags |= TF_USED;
  136. t->identifier_len = 0;
  137. t->target_state = TS_PRESENT;
  138. return t;
  139. }
  140. struct esas2r_target *esas2r_targ_db_add_pthru(struct esas2r_adapter *a,
  141. struct esas2r_disc_context *dc,
  142. u8 *ident,
  143. u8 ident_len)
  144. {
  145. struct esas2r_target *t;
  146. esas2r_trace_enter();
  147. if (dc->curr_virt_id >= ESAS2R_MAX_TARGETS) {
  148. esas2r_bugon();
  149. esas2r_trace_exit();
  150. return NULL;
  151. }
  152. /* see if we found this device before. */
  153. t = esas2r_targ_db_find_by_ident(a, ident, ident_len);
  154. if (t == NULL) {
  155. t = a->targetdb + dc->curr_virt_id;
  156. if (ident_len > sizeof(t->identifier)
  157. || t->target_state == TS_PRESENT) {
  158. esas2r_trace_exit();
  159. return NULL;
  160. }
  161. }
  162. esas2r_hdebug("add PT; T:%d, V:%d, P:%d", esas2r_targ_get_id(t, a),
  163. dc->curr_virt_id,
  164. dc->curr_phys_id);
  165. t->block_size = 0;
  166. t->inter_byte = 0;
  167. t->inter_block = 0;
  168. t->virt_targ_id = dc->curr_virt_id;
  169. t->phys_targ_id = dc->curr_phys_id;
  170. t->identifier_len = ident_len;
  171. memcpy(t->identifier, ident, ident_len);
  172. t->flags |= TF_PASS_THRU | TF_USED;
  173. t->target_state = TS_PRESENT;
  174. return t;
  175. }
  176. void esas2r_targ_db_remove(struct esas2r_adapter *a, struct esas2r_target *t)
  177. {
  178. esas2r_trace_enter();
  179. t->target_state = TS_NOT_PRESENT;
  180. esas2r_trace("remove id:%d", esas2r_targ_get_id(t, a));
  181. esas2r_trace_exit();
  182. }
  183. struct esas2r_target *esas2r_targ_db_find_by_sas_addr(struct esas2r_adapter *a,
  184. u64 *sas_addr)
  185. {
  186. struct esas2r_target *t;
  187. for (t = a->targetdb; t < a->targetdb_end; t++)
  188. if (t->sas_addr == *sas_addr)
  189. return t;
  190. return NULL;
  191. }
  192. struct esas2r_target *esas2r_targ_db_find_by_ident(struct esas2r_adapter *a,
  193. void *identifier,
  194. u8 ident_len)
  195. {
  196. struct esas2r_target *t;
  197. for (t = a->targetdb; t < a->targetdb_end; t++) {
  198. if (ident_len == t->identifier_len
  199. && memcmp(&t->identifier[0], identifier,
  200. ident_len) == 0)
  201. return t;
  202. }
  203. return NULL;
  204. }
  205. u16 esas2r_targ_db_find_next_present(struct esas2r_adapter *a, u16 target_id)
  206. {
  207. u16 id = target_id + 1;
  208. while (id < ESAS2R_MAX_TARGETS) {
  209. struct esas2r_target *t = a->targetdb + id;
  210. if (t->target_state == TS_PRESENT)
  211. break;
  212. id++;
  213. }
  214. return id;
  215. }
  216. struct esas2r_target *esas2r_targ_db_find_by_virt_id(struct esas2r_adapter *a,
  217. u16 virt_id)
  218. {
  219. struct esas2r_target *t;
  220. for (t = a->targetdb; t < a->targetdb_end; t++) {
  221. if (t->target_state != TS_PRESENT)
  222. continue;
  223. if (t->virt_targ_id == virt_id)
  224. return t;
  225. }
  226. return NULL;
  227. }
  228. u16 esas2r_targ_db_get_tgt_cnt(struct esas2r_adapter *a)
  229. {
  230. u16 devcnt = 0;
  231. struct esas2r_target *t;
  232. unsigned long flags;
  233. spin_lock_irqsave(&a->mem_lock, flags);
  234. for (t = a->targetdb; t < a->targetdb_end; t++)
  235. if (t->target_state == TS_PRESENT)
  236. devcnt++;
  237. spin_unlock_irqrestore(&a->mem_lock, flags);
  238. return devcnt;
  239. }