host.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510
  1. /*
  2. * This file is provided under a dual BSD/GPLv2 license. When using or
  3. * redistributing this file, you may do so under either license.
  4. *
  5. * GPL LICENSE SUMMARY
  6. *
  7. * Copyright(c) 2008 - 2011 Intel Corporation. All rights reserved.
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of version 2 of the GNU General Public License as
  11. * published by the Free Software Foundation.
  12. *
  13. * This program is distributed in the hope that it will be useful, but
  14. * WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
  21. * The full GNU General Public License is included in this distribution
  22. * in the file called LICENSE.GPL.
  23. *
  24. * BSD LICENSE
  25. *
  26. * Copyright(c) 2008 - 2011 Intel Corporation. All rights reserved.
  27. * All rights reserved.
  28. *
  29. * Redistribution and use in source and binary forms, with or without
  30. * modification, are permitted provided that the following conditions
  31. * are met:
  32. *
  33. * * Redistributions of source code must retain the above copyright
  34. * notice, this list of conditions and the following disclaimer.
  35. * * Redistributions in binary form must reproduce the above copyright
  36. * notice, this list of conditions and the following disclaimer in
  37. * the documentation and/or other materials provided with the
  38. * distribution.
  39. * * Neither the name of Intel Corporation nor the names of its
  40. * contributors may be used to endorse or promote products derived
  41. * from this software without specific prior written permission.
  42. *
  43. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  44. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  45. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  46. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  47. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  48. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  49. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  50. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  51. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  52. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  53. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  54. */
  55. #ifndef _SCI_HOST_H_
  56. #define _SCI_HOST_H_
  57. #include <scsi/sas_ata.h>
  58. #include "remote_device.h"
  59. #include "phy.h"
  60. #include "isci.h"
  61. #include "remote_node_table.h"
  62. #include "registers.h"
  63. #include "unsolicited_frame_control.h"
  64. #include "probe_roms.h"
  65. struct isci_request;
  66. struct scu_task_context;
  67. /**
  68. * struct sci_power_control -
  69. *
  70. * This structure defines the fields for managing power control for direct
  71. * attached disk devices.
  72. */
  73. struct sci_power_control {
  74. /**
  75. * This field is set when the power control timer is running and cleared when
  76. * it is not.
  77. */
  78. bool timer_started;
  79. /**
  80. * Timer to control when the directed attached disks can consume power.
  81. */
  82. struct sci_timer timer;
  83. /**
  84. * This field is used to keep track of how many phys are put into the
  85. * requesters field.
  86. */
  87. u8 phys_waiting;
  88. /**
  89. * This field is used to keep track of how many phys have been granted to consume power
  90. */
  91. u8 phys_granted_power;
  92. /**
  93. * This field is an array of phys that we are waiting on. The phys are direct
  94. * mapped into requesters via struct sci_phy.phy_index
  95. */
  96. struct isci_phy *requesters[SCI_MAX_PHYS];
  97. };
  98. struct sci_port_configuration_agent;
  99. typedef void (*port_config_fn)(struct isci_host *,
  100. struct sci_port_configuration_agent *,
  101. struct isci_port *, struct isci_phy *);
  102. bool is_port_config_apc(struct isci_host *ihost);
  103. bool is_controller_start_complete(struct isci_host *ihost);
  104. struct sci_port_configuration_agent {
  105. u16 phy_configured_mask;
  106. u16 phy_ready_mask;
  107. struct {
  108. u8 min_index;
  109. u8 max_index;
  110. } phy_valid_port_range[SCI_MAX_PHYS];
  111. bool timer_pending;
  112. port_config_fn link_up_handler;
  113. port_config_fn link_down_handler;
  114. struct sci_timer timer;
  115. };
  116. /**
  117. * isci_host - primary host/controller object
  118. * @timer: timeout start/stop operations
  119. * @device_table: rni (hw remote node index) to remote device lookup table
  120. * @available_remote_nodes: rni allocator
  121. * @power_control: manage device spin up
  122. * @io_request_sequence: generation number for tci's (task contexts)
  123. * @task_context_table: hw task context table
  124. * @remote_node_context_table: hw remote node context table
  125. * @completion_queue: hw-producer driver-consumer communication ring
  126. * @completion_queue_get: tracks the driver 'head' of the ring to notify hw
  127. * @logical_port_entries: min({driver|silicon}-supported-port-count)
  128. * @remote_node_entries: min({driver|silicon}-supported-node-count)
  129. * @task_context_entries: min({driver|silicon}-supported-task-count)
  130. * @phy_timer: phy startup timer
  131. * @invalid_phy_mask: if an invalid_link_up notification is reported a bit for
  132. * the phy index is set so further notifications are not
  133. * made. Once the phy reports link up and is made part of a
  134. * port then this bit is cleared.
  135. */
  136. struct isci_host {
  137. struct sci_base_state_machine sm;
  138. /* XXX can we time this externally */
  139. struct sci_timer timer;
  140. /* XXX drop reference module params directly */
  141. struct sci_user_parameters user_parameters;
  142. /* XXX no need to be a union */
  143. struct sci_oem_params oem_parameters;
  144. struct sci_port_configuration_agent port_agent;
  145. struct isci_remote_device *device_table[SCI_MAX_REMOTE_DEVICES];
  146. struct sci_remote_node_table available_remote_nodes;
  147. struct sci_power_control power_control;
  148. u8 io_request_sequence[SCI_MAX_IO_REQUESTS];
  149. struct scu_task_context *task_context_table;
  150. dma_addr_t tc_dma;
  151. union scu_remote_node_context *remote_node_context_table;
  152. dma_addr_t rnc_dma;
  153. u32 *completion_queue;
  154. dma_addr_t cq_dma;
  155. u32 completion_queue_get;
  156. u32 logical_port_entries;
  157. u32 remote_node_entries;
  158. u32 task_context_entries;
  159. void *ufi_buf;
  160. dma_addr_t ufi_dma;
  161. struct sci_unsolicited_frame_control uf_control;
  162. /* phy startup */
  163. struct sci_timer phy_timer;
  164. /* XXX kill */
  165. bool phy_startup_timer_pending;
  166. u32 next_phy_to_start;
  167. /* XXX convert to unsigned long and use bitops */
  168. u8 invalid_phy_mask;
  169. /* TODO attempt dynamic interrupt coalescing scheme */
  170. u16 interrupt_coalesce_number;
  171. u32 interrupt_coalesce_timeout;
  172. struct smu_registers __iomem *smu_registers;
  173. struct scu_registers __iomem *scu_registers;
  174. u16 tci_head;
  175. u16 tci_tail;
  176. u16 tci_pool[SCI_MAX_IO_REQUESTS];
  177. int id; /* unique within a given pci device */
  178. struct isci_phy phys[SCI_MAX_PHYS];
  179. struct isci_port ports[SCI_MAX_PORTS + 1]; /* includes dummy port */
  180. struct asd_sas_port sas_ports[SCI_MAX_PORTS];
  181. struct sas_ha_struct sas_ha;
  182. struct pci_dev *pdev;
  183. #define IHOST_START_PENDING 0
  184. #define IHOST_STOP_PENDING 1
  185. #define IHOST_IRQ_ENABLED 2
  186. unsigned long flags;
  187. wait_queue_head_t eventq;
  188. struct Scsi_Host *shost;
  189. struct tasklet_struct completion_tasklet;
  190. struct list_head requests_to_complete;
  191. struct list_head requests_to_errorback;
  192. spinlock_t scic_lock;
  193. struct isci_request *reqs[SCI_MAX_IO_REQUESTS];
  194. struct isci_remote_device devices[SCI_MAX_REMOTE_DEVICES];
  195. };
  196. /**
  197. * enum sci_controller_states - This enumeration depicts all the states
  198. * for the common controller state machine.
  199. */
  200. enum sci_controller_states {
  201. /**
  202. * Simply the initial state for the base controller state machine.
  203. */
  204. SCIC_INITIAL = 0,
  205. /**
  206. * This state indicates that the controller is reset. The memory for
  207. * the controller is in it's initial state, but the controller requires
  208. * initialization.
  209. * This state is entered from the INITIAL state.
  210. * This state is entered from the RESETTING state.
  211. */
  212. SCIC_RESET,
  213. /**
  214. * This state is typically an action state that indicates the controller
  215. * is in the process of initialization. In this state no new IO operations
  216. * are permitted.
  217. * This state is entered from the RESET state.
  218. */
  219. SCIC_INITIALIZING,
  220. /**
  221. * This state indicates that the controller has been successfully
  222. * initialized. In this state no new IO operations are permitted.
  223. * This state is entered from the INITIALIZING state.
  224. */
  225. SCIC_INITIALIZED,
  226. /**
  227. * This state indicates the the controller is in the process of becoming
  228. * ready (i.e. starting). In this state no new IO operations are permitted.
  229. * This state is entered from the INITIALIZED state.
  230. */
  231. SCIC_STARTING,
  232. /**
  233. * This state indicates the controller is now ready. Thus, the user
  234. * is able to perform IO operations on the controller.
  235. * This state is entered from the STARTING state.
  236. */
  237. SCIC_READY,
  238. /**
  239. * This state is typically an action state that indicates the controller
  240. * is in the process of resetting. Thus, the user is unable to perform
  241. * IO operations on the controller. A reset is considered destructive in
  242. * most cases.
  243. * This state is entered from the READY state.
  244. * This state is entered from the FAILED state.
  245. * This state is entered from the STOPPED state.
  246. */
  247. SCIC_RESETTING,
  248. /**
  249. * This state indicates that the controller is in the process of stopping.
  250. * In this state no new IO operations are permitted, but existing IO
  251. * operations are allowed to complete.
  252. * This state is entered from the READY state.
  253. */
  254. SCIC_STOPPING,
  255. /**
  256. * This state indicates that the controller could not successfully be
  257. * initialized. In this state no new IO operations are permitted.
  258. * This state is entered from the INITIALIZING state.
  259. * This state is entered from the STARTING state.
  260. * This state is entered from the STOPPING state.
  261. * This state is entered from the RESETTING state.
  262. */
  263. SCIC_FAILED,
  264. };
  265. /**
  266. * struct isci_pci_info - This class represents the pci function containing the
  267. * controllers. Depending on PCI SKU, there could be up to 2 controllers in
  268. * the PCI function.
  269. */
  270. #define SCI_MAX_MSIX_INT (SCI_NUM_MSI_X_INT*SCI_MAX_CONTROLLERS)
  271. struct isci_pci_info {
  272. struct msix_entry msix_entries[SCI_MAX_MSIX_INT];
  273. struct isci_host *hosts[SCI_MAX_CONTROLLERS];
  274. struct isci_orom *orom;
  275. };
  276. static inline struct isci_pci_info *to_pci_info(struct pci_dev *pdev)
  277. {
  278. return pci_get_drvdata(pdev);
  279. }
  280. #define for_each_isci_host(id, ihost, pdev) \
  281. for (id = 0, ihost = to_pci_info(pdev)->hosts[id]; \
  282. id < ARRAY_SIZE(to_pci_info(pdev)->hosts) && ihost; \
  283. ihost = to_pci_info(pdev)->hosts[++id])
  284. static inline void wait_for_start(struct isci_host *ihost)
  285. {
  286. wait_event(ihost->eventq, !test_bit(IHOST_START_PENDING, &ihost->flags));
  287. }
  288. static inline void wait_for_stop(struct isci_host *ihost)
  289. {
  290. wait_event(ihost->eventq, !test_bit(IHOST_STOP_PENDING, &ihost->flags));
  291. }
  292. static inline void wait_for_device_start(struct isci_host *ihost, struct isci_remote_device *idev)
  293. {
  294. wait_event(ihost->eventq, !test_bit(IDEV_START_PENDING, &idev->flags));
  295. }
  296. static inline void wait_for_device_stop(struct isci_host *ihost, struct isci_remote_device *idev)
  297. {
  298. wait_event(ihost->eventq, !test_bit(IDEV_STOP_PENDING, &idev->flags));
  299. }
  300. static inline struct isci_host *dev_to_ihost(struct domain_device *dev)
  301. {
  302. return dev->port->ha->lldd_ha;
  303. }
  304. /* we always use protocol engine group zero */
  305. #define ISCI_PEG 0
  306. /* see sci_controller_io_tag_allocate|free for how seq and tci are built */
  307. #define ISCI_TAG(seq, tci) (((u16) (seq)) << 12 | tci)
  308. /* these are returned by the hardware, so sanitize them */
  309. #define ISCI_TAG_SEQ(tag) (((tag) >> 12) & (SCI_MAX_SEQ-1))
  310. #define ISCI_TAG_TCI(tag) ((tag) & (SCI_MAX_IO_REQUESTS-1))
  311. /* interrupt coalescing baseline: 9 == 3 to 5us interrupt delay per command */
  312. #define ISCI_COALESCE_BASE 9
  313. /* expander attached sata devices require 3 rnc slots */
  314. static inline int sci_remote_device_node_count(struct isci_remote_device *idev)
  315. {
  316. struct domain_device *dev = idev->domain_dev;
  317. if (dev_is_sata(dev) && dev->parent)
  318. return SCU_STP_REMOTE_NODE_COUNT;
  319. return SCU_SSP_REMOTE_NODE_COUNT;
  320. }
  321. /**
  322. * sci_controller_clear_invalid_phy() -
  323. *
  324. * This macro will clear the bit in the invalid phy mask for this controller
  325. * object. This is used to control messages reported for invalid link up
  326. * notifications.
  327. */
  328. #define sci_controller_clear_invalid_phy(controller, phy) \
  329. ((controller)->invalid_phy_mask &= ~(1 << (phy)->phy_index))
  330. static inline struct device *scirdev_to_dev(struct isci_remote_device *idev)
  331. {
  332. if (!idev || !idev->isci_port || !idev->isci_port->isci_host)
  333. return NULL;
  334. return &idev->isci_port->isci_host->pdev->dev;
  335. }
  336. static inline bool is_a2(struct pci_dev *pdev)
  337. {
  338. if (pdev->revision < 4)
  339. return true;
  340. return false;
  341. }
  342. static inline bool is_b0(struct pci_dev *pdev)
  343. {
  344. if (pdev->revision == 4)
  345. return true;
  346. return false;
  347. }
  348. static inline bool is_c0(struct pci_dev *pdev)
  349. {
  350. if (pdev->revision == 5)
  351. return true;
  352. return false;
  353. }
  354. static inline bool is_c1(struct pci_dev *pdev)
  355. {
  356. if (pdev->revision >= 6)
  357. return true;
  358. return false;
  359. }
  360. enum cable_selections {
  361. short_cable = 0,
  362. long_cable = 1,
  363. medium_cable = 2,
  364. undefined_cable = 3
  365. };
  366. #define CABLE_OVERRIDE_DISABLED (0x10000)
  367. static inline int is_cable_select_overridden(void)
  368. {
  369. return cable_selection_override < CABLE_OVERRIDE_DISABLED;
  370. }
  371. enum cable_selections decode_cable_selection(struct isci_host *ihost, int phy);
  372. void validate_cable_selections(struct isci_host *ihost);
  373. char *lookup_cable_names(enum cable_selections);
  374. /* set hw control for 'activity', even though active enclosures seem to drive
  375. * the activity led on their own. Skip setting FSENG control on 'status' due
  376. * to unexpected operation and 'error' due to not being a supported automatic
  377. * FSENG output
  378. */
  379. #define SGPIO_HW_CONTROL 0x00000443
  380. static inline int isci_gpio_count(struct isci_host *ihost)
  381. {
  382. return ARRAY_SIZE(ihost->scu_registers->peg0.sgpio.output_data_select);
  383. }
  384. void sci_controller_post_request(struct isci_host *ihost,
  385. u32 request);
  386. void sci_controller_release_frame(struct isci_host *ihost,
  387. u32 frame_index);
  388. void sci_controller_copy_sata_response(void *response_buffer,
  389. void *frame_header,
  390. void *frame_buffer);
  391. enum sci_status sci_controller_allocate_remote_node_context(struct isci_host *ihost,
  392. struct isci_remote_device *idev,
  393. u16 *node_id);
  394. void sci_controller_free_remote_node_context(
  395. struct isci_host *ihost,
  396. struct isci_remote_device *idev,
  397. u16 node_id);
  398. struct isci_request *sci_request_by_tag(struct isci_host *ihost, u16 io_tag);
  399. void sci_controller_power_control_queue_insert(struct isci_host *ihost,
  400. struct isci_phy *iphy);
  401. void sci_controller_power_control_queue_remove(struct isci_host *ihost,
  402. struct isci_phy *iphy);
  403. void sci_controller_link_up(struct isci_host *ihost, struct isci_port *iport,
  404. struct isci_phy *iphy);
  405. void sci_controller_link_down(struct isci_host *ihost, struct isci_port *iport,
  406. struct isci_phy *iphy);
  407. void sci_controller_remote_device_stopped(struct isci_host *ihost,
  408. struct isci_remote_device *idev);
  409. enum sci_status sci_controller_continue_io(struct isci_request *ireq);
  410. int isci_host_scan_finished(struct Scsi_Host *, unsigned long);
  411. void isci_host_scan_start(struct Scsi_Host *);
  412. u16 isci_alloc_tag(struct isci_host *ihost);
  413. enum sci_status isci_free_tag(struct isci_host *ihost, u16 io_tag);
  414. void isci_tci_free(struct isci_host *ihost, u16 tci);
  415. int isci_host_init(struct isci_host *);
  416. void isci_host_completion_routine(unsigned long data);
  417. void isci_host_deinit(struct isci_host *);
  418. void sci_controller_disable_interrupts(struct isci_host *ihost);
  419. bool sci_controller_has_remote_devices_stopping(struct isci_host *ihost);
  420. void sci_controller_transition_to_ready(struct isci_host *ihost, enum sci_status status);
  421. enum sci_status sci_controller_start_io(
  422. struct isci_host *ihost,
  423. struct isci_remote_device *idev,
  424. struct isci_request *ireq);
  425. enum sci_task_status sci_controller_start_task(
  426. struct isci_host *ihost,
  427. struct isci_remote_device *idev,
  428. struct isci_request *ireq);
  429. enum sci_status sci_controller_terminate_request(
  430. struct isci_host *ihost,
  431. struct isci_remote_device *idev,
  432. struct isci_request *ireq);
  433. enum sci_status sci_controller_complete_io(
  434. struct isci_host *ihost,
  435. struct isci_remote_device *idev,
  436. struct isci_request *ireq);
  437. void sci_port_configuration_agent_construct(
  438. struct sci_port_configuration_agent *port_agent);
  439. enum sci_status sci_port_configuration_agent_initialize(
  440. struct isci_host *ihost,
  441. struct sci_port_configuration_agent *port_agent);
  442. int isci_gpio_write(struct sas_ha_struct *, u8 reg_type, u8 reg_index,
  443. u8 reg_count, u8 *write_data);
  444. #endif