esas2r_init.c 46 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746
  1. /*
  2. * linux/drivers/scsi/esas2r/esas2r_init.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)mpt3sas/mpt3sas_trigger_diag.
  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. static bool esas2r_initmem_alloc(struct esas2r_adapter *a,
  45. struct esas2r_mem_desc *mem_desc,
  46. u32 align)
  47. {
  48. mem_desc->esas2r_param = mem_desc->size + align;
  49. mem_desc->virt_addr = NULL;
  50. mem_desc->phys_addr = 0;
  51. mem_desc->esas2r_data = dma_alloc_coherent(&a->pcid->dev,
  52. (size_t)mem_desc->
  53. esas2r_param,
  54. (dma_addr_t *)&mem_desc->
  55. phys_addr,
  56. GFP_KERNEL);
  57. if (mem_desc->esas2r_data == NULL) {
  58. esas2r_log(ESAS2R_LOG_CRIT,
  59. "failed to allocate %lu bytes of consistent memory!",
  60. (long
  61. unsigned
  62. int)mem_desc->esas2r_param);
  63. return false;
  64. }
  65. mem_desc->virt_addr = PTR_ALIGN(mem_desc->esas2r_data, align);
  66. mem_desc->phys_addr = ALIGN(mem_desc->phys_addr, align);
  67. memset(mem_desc->virt_addr, 0, mem_desc->size);
  68. return true;
  69. }
  70. static void esas2r_initmem_free(struct esas2r_adapter *a,
  71. struct esas2r_mem_desc *mem_desc)
  72. {
  73. if (mem_desc->virt_addr == NULL)
  74. return;
  75. /*
  76. * Careful! phys_addr and virt_addr may have been adjusted from the
  77. * original allocation in order to return the desired alignment. That
  78. * means we have to use the original address (in esas2r_data) and size
  79. * (esas2r_param) and calculate the original physical address based on
  80. * the difference between the requested and actual allocation size.
  81. */
  82. if (mem_desc->phys_addr) {
  83. int unalign = ((u8 *)mem_desc->virt_addr) -
  84. ((u8 *)mem_desc->esas2r_data);
  85. dma_free_coherent(&a->pcid->dev,
  86. (size_t)mem_desc->esas2r_param,
  87. mem_desc->esas2r_data,
  88. (dma_addr_t)(mem_desc->phys_addr - unalign));
  89. } else {
  90. kfree(mem_desc->esas2r_data);
  91. }
  92. mem_desc->virt_addr = NULL;
  93. }
  94. static bool alloc_vda_req(struct esas2r_adapter *a,
  95. struct esas2r_request *rq)
  96. {
  97. struct esas2r_mem_desc *memdesc = kzalloc(
  98. sizeof(struct esas2r_mem_desc), GFP_KERNEL);
  99. if (memdesc == NULL) {
  100. esas2r_hdebug("could not alloc mem for vda request memdesc\n");
  101. return false;
  102. }
  103. memdesc->size = sizeof(union atto_vda_req) +
  104. ESAS2R_DATA_BUF_LEN;
  105. if (!esas2r_initmem_alloc(a, memdesc, 256)) {
  106. esas2r_hdebug("could not alloc mem for vda request\n");
  107. kfree(memdesc);
  108. return false;
  109. }
  110. a->num_vrqs++;
  111. list_add(&memdesc->next_desc, &a->vrq_mds_head);
  112. rq->vrq_md = memdesc;
  113. rq->vrq = (union atto_vda_req *)memdesc->virt_addr;
  114. rq->vrq->scsi.handle = a->num_vrqs;
  115. return true;
  116. }
  117. static void esas2r_unmap_regions(struct esas2r_adapter *a)
  118. {
  119. if (a->regs)
  120. iounmap((void __iomem *)a->regs);
  121. a->regs = NULL;
  122. pci_release_region(a->pcid, 2);
  123. if (a->data_window)
  124. iounmap((void __iomem *)a->data_window);
  125. a->data_window = NULL;
  126. pci_release_region(a->pcid, 0);
  127. }
  128. static int esas2r_map_regions(struct esas2r_adapter *a)
  129. {
  130. int error;
  131. a->regs = NULL;
  132. a->data_window = NULL;
  133. error = pci_request_region(a->pcid, 2, a->name);
  134. if (error != 0) {
  135. esas2r_log(ESAS2R_LOG_CRIT,
  136. "pci_request_region(2) failed, error %d",
  137. error);
  138. return error;
  139. }
  140. a->regs = (void __force *)ioremap(pci_resource_start(a->pcid, 2),
  141. pci_resource_len(a->pcid, 2));
  142. if (a->regs == NULL) {
  143. esas2r_log(ESAS2R_LOG_CRIT,
  144. "ioremap failed for regs mem region\n");
  145. pci_release_region(a->pcid, 2);
  146. return -EFAULT;
  147. }
  148. error = pci_request_region(a->pcid, 0, a->name);
  149. if (error != 0) {
  150. esas2r_log(ESAS2R_LOG_CRIT,
  151. "pci_request_region(2) failed, error %d",
  152. error);
  153. esas2r_unmap_regions(a);
  154. return error;
  155. }
  156. a->data_window = (void __force *)ioremap(pci_resource_start(a->pcid,
  157. 0),
  158. pci_resource_len(a->pcid, 0));
  159. if (a->data_window == NULL) {
  160. esas2r_log(ESAS2R_LOG_CRIT,
  161. "ioremap failed for data_window mem region\n");
  162. esas2r_unmap_regions(a);
  163. return -EFAULT;
  164. }
  165. return 0;
  166. }
  167. static void esas2r_setup_interrupts(struct esas2r_adapter *a, int intr_mode)
  168. {
  169. int i;
  170. /* Set up interrupt mode based on the requested value */
  171. switch (intr_mode) {
  172. case INTR_MODE_LEGACY:
  173. use_legacy_interrupts:
  174. a->intr_mode = INTR_MODE_LEGACY;
  175. break;
  176. case INTR_MODE_MSI:
  177. i = pci_enable_msi(a->pcid);
  178. if (i != 0) {
  179. esas2r_log(ESAS2R_LOG_WARN,
  180. "failed to enable MSI for adapter %d, "
  181. "falling back to legacy interrupts "
  182. "(err=%d)", a->index,
  183. i);
  184. goto use_legacy_interrupts;
  185. }
  186. a->intr_mode = INTR_MODE_MSI;
  187. set_bit(AF2_MSI_ENABLED, &a->flags2);
  188. break;
  189. default:
  190. esas2r_log(ESAS2R_LOG_WARN,
  191. "unknown interrupt_mode %d requested, "
  192. "falling back to legacy interrupt",
  193. interrupt_mode);
  194. goto use_legacy_interrupts;
  195. }
  196. }
  197. static void esas2r_claim_interrupts(struct esas2r_adapter *a)
  198. {
  199. unsigned long flags = 0;
  200. if (a->intr_mode == INTR_MODE_LEGACY)
  201. flags |= IRQF_SHARED;
  202. esas2r_log(ESAS2R_LOG_INFO,
  203. "esas2r_claim_interrupts irq=%d (%p, %s, %lx)",
  204. a->pcid->irq, a, a->name, flags);
  205. if (request_irq(a->pcid->irq,
  206. (a->intr_mode ==
  207. INTR_MODE_LEGACY) ? esas2r_interrupt :
  208. esas2r_msi_interrupt,
  209. flags,
  210. a->name,
  211. a)) {
  212. esas2r_log(ESAS2R_LOG_CRIT, "unable to request IRQ %02X",
  213. a->pcid->irq);
  214. return;
  215. }
  216. set_bit(AF2_IRQ_CLAIMED, &a->flags2);
  217. esas2r_log(ESAS2R_LOG_INFO,
  218. "claimed IRQ %d flags: 0x%lx",
  219. a->pcid->irq, flags);
  220. }
  221. int esas2r_init_adapter(struct Scsi_Host *host, struct pci_dev *pcid,
  222. int index)
  223. {
  224. struct esas2r_adapter *a;
  225. u64 bus_addr = 0;
  226. int i;
  227. void *next_uncached;
  228. struct esas2r_request *first_request, *last_request;
  229. if (index >= MAX_ADAPTERS) {
  230. esas2r_log(ESAS2R_LOG_CRIT,
  231. "tried to init invalid adapter index %u!",
  232. index);
  233. return 0;
  234. }
  235. if (esas2r_adapters[index]) {
  236. esas2r_log(ESAS2R_LOG_CRIT,
  237. "tried to init existing adapter index %u!",
  238. index);
  239. return 0;
  240. }
  241. a = (struct esas2r_adapter *)host->hostdata;
  242. memset(a, 0, sizeof(struct esas2r_adapter));
  243. a->pcid = pcid;
  244. a->host = host;
  245. if (sizeof(dma_addr_t) > 4) {
  246. const uint64_t required_mask = dma_get_required_mask
  247. (&pcid->dev);
  248. if (required_mask > DMA_BIT_MASK(32)
  249. && !pci_set_dma_mask(pcid, DMA_BIT_MASK(64))
  250. && !pci_set_consistent_dma_mask(pcid,
  251. DMA_BIT_MASK(64))) {
  252. esas2r_log_dev(ESAS2R_LOG_INFO,
  253. &(a->pcid->dev),
  254. "64-bit PCI addressing enabled\n");
  255. } else if (!pci_set_dma_mask(pcid, DMA_BIT_MASK(32))
  256. && !pci_set_consistent_dma_mask(pcid,
  257. DMA_BIT_MASK(32))) {
  258. esas2r_log_dev(ESAS2R_LOG_INFO,
  259. &(a->pcid->dev),
  260. "32-bit PCI addressing enabled\n");
  261. } else {
  262. esas2r_log(ESAS2R_LOG_CRIT,
  263. "failed to set DMA mask");
  264. esas2r_kill_adapter(index);
  265. return 0;
  266. }
  267. } else {
  268. if (!pci_set_dma_mask(pcid, DMA_BIT_MASK(32))
  269. && !pci_set_consistent_dma_mask(pcid,
  270. DMA_BIT_MASK(32))) {
  271. esas2r_log_dev(ESAS2R_LOG_INFO,
  272. &(a->pcid->dev),
  273. "32-bit PCI addressing enabled\n");
  274. } else {
  275. esas2r_log(ESAS2R_LOG_CRIT,
  276. "failed to set DMA mask");
  277. esas2r_kill_adapter(index);
  278. return 0;
  279. }
  280. }
  281. esas2r_adapters[index] = a;
  282. sprintf(a->name, ESAS2R_DRVR_NAME "_%02d", index);
  283. esas2r_debug("new adapter %p, name %s", a, a->name);
  284. spin_lock_init(&a->request_lock);
  285. spin_lock_init(&a->fw_event_lock);
  286. mutex_init(&a->fm_api_mutex);
  287. mutex_init(&a->fs_api_mutex);
  288. sema_init(&a->nvram_semaphore, 1);
  289. esas2r_fw_event_off(a);
  290. snprintf(a->fw_event_q_name, ESAS2R_KOBJ_NAME_LEN, "esas2r/%d",
  291. a->index);
  292. a->fw_event_q = create_singlethread_workqueue(a->fw_event_q_name);
  293. init_waitqueue_head(&a->buffered_ioctl_waiter);
  294. init_waitqueue_head(&a->nvram_waiter);
  295. init_waitqueue_head(&a->fm_api_waiter);
  296. init_waitqueue_head(&a->fs_api_waiter);
  297. init_waitqueue_head(&a->vda_waiter);
  298. INIT_LIST_HEAD(&a->general_req.req_list);
  299. INIT_LIST_HEAD(&a->active_list);
  300. INIT_LIST_HEAD(&a->defer_list);
  301. INIT_LIST_HEAD(&a->free_sg_list_head);
  302. INIT_LIST_HEAD(&a->avail_request);
  303. INIT_LIST_HEAD(&a->vrq_mds_head);
  304. INIT_LIST_HEAD(&a->fw_event_list);
  305. first_request = (struct esas2r_request *)((u8 *)(a + 1));
  306. for (last_request = first_request, i = 1; i < num_requests;
  307. last_request++, i++) {
  308. INIT_LIST_HEAD(&last_request->req_list);
  309. list_add_tail(&last_request->comp_list, &a->avail_request);
  310. if (!alloc_vda_req(a, last_request)) {
  311. esas2r_log(ESAS2R_LOG_CRIT,
  312. "failed to allocate a VDA request!");
  313. esas2r_kill_adapter(index);
  314. return 0;
  315. }
  316. }
  317. esas2r_debug("requests: %p to %p (%d, %d)", first_request,
  318. last_request,
  319. sizeof(*first_request),
  320. num_requests);
  321. if (esas2r_map_regions(a) != 0) {
  322. esas2r_log(ESAS2R_LOG_CRIT, "could not map PCI regions!");
  323. esas2r_kill_adapter(index);
  324. return 0;
  325. }
  326. a->index = index;
  327. /* interrupts will be disabled until we are done with init */
  328. atomic_inc(&a->dis_ints_cnt);
  329. atomic_inc(&a->disable_cnt);
  330. set_bit(AF_CHPRST_PENDING, &a->flags);
  331. set_bit(AF_DISC_PENDING, &a->flags);
  332. set_bit(AF_FIRST_INIT, &a->flags);
  333. set_bit(AF_LEGACY_SGE_MODE, &a->flags);
  334. a->init_msg = ESAS2R_INIT_MSG_START;
  335. a->max_vdareq_size = 128;
  336. a->build_sgl = esas2r_build_sg_list_sge;
  337. esas2r_setup_interrupts(a, interrupt_mode);
  338. a->uncached_size = esas2r_get_uncached_size(a);
  339. a->uncached = dma_alloc_coherent(&pcid->dev,
  340. (size_t)a->uncached_size,
  341. (dma_addr_t *)&bus_addr,
  342. GFP_KERNEL);
  343. if (a->uncached == NULL) {
  344. esas2r_log(ESAS2R_LOG_CRIT,
  345. "failed to allocate %d bytes of consistent memory!",
  346. a->uncached_size);
  347. esas2r_kill_adapter(index);
  348. return 0;
  349. }
  350. a->uncached_phys = bus_addr;
  351. esas2r_debug("%d bytes uncached memory allocated @ %p (%x:%x)",
  352. a->uncached_size,
  353. a->uncached,
  354. upper_32_bits(bus_addr),
  355. lower_32_bits(bus_addr));
  356. memset(a->uncached, 0, a->uncached_size);
  357. next_uncached = a->uncached;
  358. if (!esas2r_init_adapter_struct(a,
  359. &next_uncached)) {
  360. esas2r_log(ESAS2R_LOG_CRIT,
  361. "failed to initialize adapter structure (2)!");
  362. esas2r_kill_adapter(index);
  363. return 0;
  364. }
  365. tasklet_init(&a->tasklet,
  366. esas2r_adapter_tasklet,
  367. (unsigned long)a);
  368. /*
  369. * Disable chip interrupts to prevent spurious interrupts
  370. * until we claim the IRQ.
  371. */
  372. esas2r_disable_chip_interrupts(a);
  373. esas2r_check_adapter(a);
  374. if (!esas2r_init_adapter_hw(a, true))
  375. esas2r_log(ESAS2R_LOG_CRIT, "failed to initialize hardware!");
  376. else
  377. esas2r_debug("esas2r_init_adapter ok");
  378. esas2r_claim_interrupts(a);
  379. if (test_bit(AF2_IRQ_CLAIMED, &a->flags2))
  380. esas2r_enable_chip_interrupts(a);
  381. set_bit(AF2_INIT_DONE, &a->flags2);
  382. if (!test_bit(AF_DEGRADED_MODE, &a->flags))
  383. esas2r_kickoff_timer(a);
  384. esas2r_debug("esas2r_init_adapter done for %p (%d)",
  385. a, a->disable_cnt);
  386. return 1;
  387. }
  388. static void esas2r_adapter_power_down(struct esas2r_adapter *a,
  389. int power_management)
  390. {
  391. struct esas2r_mem_desc *memdesc, *next;
  392. if ((test_bit(AF2_INIT_DONE, &a->flags2))
  393. && (!test_bit(AF_DEGRADED_MODE, &a->flags))) {
  394. if (!power_management) {
  395. del_timer_sync(&a->timer);
  396. tasklet_kill(&a->tasklet);
  397. }
  398. esas2r_power_down(a);
  399. /*
  400. * There are versions of firmware that do not handle the sync
  401. * cache command correctly. Stall here to ensure that the
  402. * cache is lazily flushed.
  403. */
  404. mdelay(500);
  405. esas2r_debug("chip halted");
  406. }
  407. /* Remove sysfs binary files */
  408. if (a->sysfs_fw_created) {
  409. sysfs_remove_bin_file(&a->host->shost_dev.kobj, &bin_attr_fw);
  410. a->sysfs_fw_created = 0;
  411. }
  412. if (a->sysfs_fs_created) {
  413. sysfs_remove_bin_file(&a->host->shost_dev.kobj, &bin_attr_fs);
  414. a->sysfs_fs_created = 0;
  415. }
  416. if (a->sysfs_vda_created) {
  417. sysfs_remove_bin_file(&a->host->shost_dev.kobj, &bin_attr_vda);
  418. a->sysfs_vda_created = 0;
  419. }
  420. if (a->sysfs_hw_created) {
  421. sysfs_remove_bin_file(&a->host->shost_dev.kobj, &bin_attr_hw);
  422. a->sysfs_hw_created = 0;
  423. }
  424. if (a->sysfs_live_nvram_created) {
  425. sysfs_remove_bin_file(&a->host->shost_dev.kobj,
  426. &bin_attr_live_nvram);
  427. a->sysfs_live_nvram_created = 0;
  428. }
  429. if (a->sysfs_default_nvram_created) {
  430. sysfs_remove_bin_file(&a->host->shost_dev.kobj,
  431. &bin_attr_default_nvram);
  432. a->sysfs_default_nvram_created = 0;
  433. }
  434. /* Clean up interrupts */
  435. if (test_bit(AF2_IRQ_CLAIMED, &a->flags2)) {
  436. esas2r_log_dev(ESAS2R_LOG_INFO,
  437. &(a->pcid->dev),
  438. "free_irq(%d) called", a->pcid->irq);
  439. free_irq(a->pcid->irq, a);
  440. esas2r_debug("IRQ released");
  441. clear_bit(AF2_IRQ_CLAIMED, &a->flags2);
  442. }
  443. if (test_bit(AF2_MSI_ENABLED, &a->flags2)) {
  444. pci_disable_msi(a->pcid);
  445. clear_bit(AF2_MSI_ENABLED, &a->flags2);
  446. esas2r_debug("MSI disabled");
  447. }
  448. if (a->inbound_list_md.virt_addr)
  449. esas2r_initmem_free(a, &a->inbound_list_md);
  450. if (a->outbound_list_md.virt_addr)
  451. esas2r_initmem_free(a, &a->outbound_list_md);
  452. list_for_each_entry_safe(memdesc, next, &a->free_sg_list_head,
  453. next_desc) {
  454. esas2r_initmem_free(a, memdesc);
  455. }
  456. /* Following frees everything allocated via alloc_vda_req */
  457. list_for_each_entry_safe(memdesc, next, &a->vrq_mds_head, next_desc) {
  458. esas2r_initmem_free(a, memdesc);
  459. list_del(&memdesc->next_desc);
  460. kfree(memdesc);
  461. }
  462. kfree(a->first_ae_req);
  463. a->first_ae_req = NULL;
  464. kfree(a->sg_list_mds);
  465. a->sg_list_mds = NULL;
  466. kfree(a->req_table);
  467. a->req_table = NULL;
  468. if (a->regs) {
  469. esas2r_unmap_regions(a);
  470. a->regs = NULL;
  471. a->data_window = NULL;
  472. esas2r_debug("regions unmapped");
  473. }
  474. }
  475. /* Release/free allocated resources for specified adapters. */
  476. void esas2r_kill_adapter(int i)
  477. {
  478. struct esas2r_adapter *a = esas2r_adapters[i];
  479. if (a) {
  480. unsigned long flags;
  481. struct workqueue_struct *wq;
  482. esas2r_debug("killing adapter %p [%d] ", a, i);
  483. esas2r_fw_event_off(a);
  484. esas2r_adapter_power_down(a, 0);
  485. if (esas2r_buffered_ioctl &&
  486. (a->pcid == esas2r_buffered_ioctl_pcid)) {
  487. dma_free_coherent(&a->pcid->dev,
  488. (size_t)esas2r_buffered_ioctl_size,
  489. esas2r_buffered_ioctl,
  490. esas2r_buffered_ioctl_addr);
  491. esas2r_buffered_ioctl = NULL;
  492. }
  493. if (a->vda_buffer) {
  494. dma_free_coherent(&a->pcid->dev,
  495. (size_t)VDA_MAX_BUFFER_SIZE,
  496. a->vda_buffer,
  497. (dma_addr_t)a->ppvda_buffer);
  498. a->vda_buffer = NULL;
  499. }
  500. if (a->fs_api_buffer) {
  501. dma_free_coherent(&a->pcid->dev,
  502. (size_t)a->fs_api_buffer_size,
  503. a->fs_api_buffer,
  504. (dma_addr_t)a->ppfs_api_buffer);
  505. a->fs_api_buffer = NULL;
  506. }
  507. kfree(a->local_atto_ioctl);
  508. a->local_atto_ioctl = NULL;
  509. spin_lock_irqsave(&a->fw_event_lock, flags);
  510. wq = a->fw_event_q;
  511. a->fw_event_q = NULL;
  512. spin_unlock_irqrestore(&a->fw_event_lock, flags);
  513. if (wq)
  514. destroy_workqueue(wq);
  515. if (a->uncached) {
  516. dma_free_coherent(&a->pcid->dev,
  517. (size_t)a->uncached_size,
  518. a->uncached,
  519. (dma_addr_t)a->uncached_phys);
  520. a->uncached = NULL;
  521. esas2r_debug("uncached area freed");
  522. }
  523. esas2r_log_dev(ESAS2R_LOG_INFO,
  524. &(a->pcid->dev),
  525. "pci_disable_device() called. msix_enabled: %d "
  526. "msi_enabled: %d irq: %d pin: %d",
  527. a->pcid->msix_enabled,
  528. a->pcid->msi_enabled,
  529. a->pcid->irq,
  530. a->pcid->pin);
  531. esas2r_log_dev(ESAS2R_LOG_INFO,
  532. &(a->pcid->dev),
  533. "before pci_disable_device() enable_cnt: %d",
  534. a->pcid->enable_cnt.counter);
  535. pci_disable_device(a->pcid);
  536. esas2r_log_dev(ESAS2R_LOG_INFO,
  537. &(a->pcid->dev),
  538. "after pci_disable_device() enable_cnt: %d",
  539. a->pcid->enable_cnt.counter);
  540. esas2r_log_dev(ESAS2R_LOG_INFO,
  541. &(a->pcid->dev),
  542. "pci_set_drv_data(%p, NULL) called",
  543. a->pcid);
  544. pci_set_drvdata(a->pcid, NULL);
  545. esas2r_adapters[i] = NULL;
  546. if (test_bit(AF2_INIT_DONE, &a->flags2)) {
  547. clear_bit(AF2_INIT_DONE, &a->flags2);
  548. set_bit(AF_DEGRADED_MODE, &a->flags);
  549. esas2r_log_dev(ESAS2R_LOG_INFO,
  550. &(a->host->shost_gendev),
  551. "scsi_remove_host() called");
  552. scsi_remove_host(a->host);
  553. esas2r_log_dev(ESAS2R_LOG_INFO,
  554. &(a->host->shost_gendev),
  555. "scsi_host_put() called");
  556. scsi_host_put(a->host);
  557. }
  558. }
  559. }
  560. int esas2r_suspend(struct pci_dev *pdev, pm_message_t state)
  561. {
  562. struct Scsi_Host *host = pci_get_drvdata(pdev);
  563. u32 device_state;
  564. struct esas2r_adapter *a = (struct esas2r_adapter *)host->hostdata;
  565. esas2r_log_dev(ESAS2R_LOG_INFO, &(pdev->dev), "suspending adapter()");
  566. if (!a)
  567. return -ENODEV;
  568. esas2r_adapter_power_down(a, 1);
  569. device_state = pci_choose_state(pdev, state);
  570. esas2r_log_dev(ESAS2R_LOG_INFO, &(pdev->dev),
  571. "pci_save_state() called");
  572. pci_save_state(pdev);
  573. esas2r_log_dev(ESAS2R_LOG_INFO, &(pdev->dev),
  574. "pci_disable_device() called");
  575. pci_disable_device(pdev);
  576. esas2r_log_dev(ESAS2R_LOG_INFO, &(pdev->dev),
  577. "pci_set_power_state() called");
  578. pci_set_power_state(pdev, device_state);
  579. esas2r_log_dev(ESAS2R_LOG_INFO, &(pdev->dev), "esas2r_suspend(): 0");
  580. return 0;
  581. }
  582. int esas2r_resume(struct pci_dev *pdev)
  583. {
  584. struct Scsi_Host *host = pci_get_drvdata(pdev);
  585. struct esas2r_adapter *a = (struct esas2r_adapter *)host->hostdata;
  586. int rez;
  587. esas2r_log_dev(ESAS2R_LOG_INFO, &(pdev->dev), "resuming adapter()");
  588. esas2r_log_dev(ESAS2R_LOG_INFO, &(pdev->dev),
  589. "pci_set_power_state(PCI_D0) "
  590. "called");
  591. pci_set_power_state(pdev, PCI_D0);
  592. esas2r_log_dev(ESAS2R_LOG_INFO, &(pdev->dev),
  593. "pci_enable_wake(PCI_D0, 0) "
  594. "called");
  595. pci_enable_wake(pdev, PCI_D0, 0);
  596. esas2r_log_dev(ESAS2R_LOG_INFO, &(pdev->dev),
  597. "pci_restore_state() called");
  598. pci_restore_state(pdev);
  599. esas2r_log_dev(ESAS2R_LOG_INFO, &(pdev->dev),
  600. "pci_enable_device() called");
  601. rez = pci_enable_device(pdev);
  602. pci_set_master(pdev);
  603. if (!a) {
  604. rez = -ENODEV;
  605. goto error_exit;
  606. }
  607. if (esas2r_map_regions(a) != 0) {
  608. esas2r_log(ESAS2R_LOG_CRIT, "could not re-map PCI regions!");
  609. rez = -ENOMEM;
  610. goto error_exit;
  611. }
  612. /* Set up interupt mode */
  613. esas2r_setup_interrupts(a, a->intr_mode);
  614. /*
  615. * Disable chip interrupts to prevent spurious interrupts until we
  616. * claim the IRQ.
  617. */
  618. esas2r_disable_chip_interrupts(a);
  619. if (!esas2r_power_up(a, true)) {
  620. esas2r_debug("yikes, esas2r_power_up failed");
  621. rez = -ENOMEM;
  622. goto error_exit;
  623. }
  624. esas2r_claim_interrupts(a);
  625. if (test_bit(AF2_IRQ_CLAIMED, &a->flags2)) {
  626. /*
  627. * Now that system interrupt(s) are claimed, we can enable
  628. * chip interrupts.
  629. */
  630. esas2r_enable_chip_interrupts(a);
  631. esas2r_kickoff_timer(a);
  632. } else {
  633. esas2r_debug("yikes, unable to claim IRQ");
  634. esas2r_log(ESAS2R_LOG_CRIT, "could not re-claim IRQ!");
  635. rez = -ENOMEM;
  636. goto error_exit;
  637. }
  638. error_exit:
  639. esas2r_log_dev(ESAS2R_LOG_CRIT, &(pdev->dev), "esas2r_resume(): %d",
  640. rez);
  641. return rez;
  642. }
  643. bool esas2r_set_degraded_mode(struct esas2r_adapter *a, char *error_str)
  644. {
  645. set_bit(AF_DEGRADED_MODE, &a->flags);
  646. esas2r_log(ESAS2R_LOG_CRIT,
  647. "setting adapter to degraded mode: %s\n", error_str);
  648. return false;
  649. }
  650. u32 esas2r_get_uncached_size(struct esas2r_adapter *a)
  651. {
  652. return sizeof(struct esas2r_sas_nvram)
  653. + ALIGN(ESAS2R_DISC_BUF_LEN, 8)
  654. + ALIGN(sizeof(u32), 8) /* outbound list copy pointer */
  655. + 8
  656. + (num_sg_lists * (u16)sgl_page_size)
  657. + ALIGN((num_requests + num_ae_requests + 1 +
  658. ESAS2R_LIST_EXTRA) *
  659. sizeof(struct esas2r_inbound_list_source_entry),
  660. 8)
  661. + ALIGN((num_requests + num_ae_requests + 1 +
  662. ESAS2R_LIST_EXTRA) *
  663. sizeof(struct atto_vda_ob_rsp), 8)
  664. + 256; /* VDA request and buffer align */
  665. }
  666. static void esas2r_init_pci_cfg_space(struct esas2r_adapter *a)
  667. {
  668. int pcie_cap_reg;
  669. pcie_cap_reg = pci_find_capability(a->pcid, PCI_CAP_ID_EXP);
  670. if (pcie_cap_reg) {
  671. u16 devcontrol;
  672. pci_read_config_word(a->pcid, pcie_cap_reg + PCI_EXP_DEVCTL,
  673. &devcontrol);
  674. if ((devcontrol & PCI_EXP_DEVCTL_READRQ) >
  675. PCI_EXP_DEVCTL_READRQ_512B) {
  676. esas2r_log(ESAS2R_LOG_INFO,
  677. "max read request size > 512B");
  678. devcontrol &= ~PCI_EXP_DEVCTL_READRQ;
  679. devcontrol |= PCI_EXP_DEVCTL_READRQ_512B;
  680. pci_write_config_word(a->pcid,
  681. pcie_cap_reg + PCI_EXP_DEVCTL,
  682. devcontrol);
  683. }
  684. }
  685. }
  686. /*
  687. * Determine the organization of the uncached data area and
  688. * finish initializing the adapter structure
  689. */
  690. bool esas2r_init_adapter_struct(struct esas2r_adapter *a,
  691. void **uncached_area)
  692. {
  693. u32 i;
  694. u8 *high;
  695. struct esas2r_inbound_list_source_entry *element;
  696. struct esas2r_request *rq;
  697. struct esas2r_mem_desc *sgl;
  698. spin_lock_init(&a->sg_list_lock);
  699. spin_lock_init(&a->mem_lock);
  700. spin_lock_init(&a->queue_lock);
  701. a->targetdb_end = &a->targetdb[ESAS2R_MAX_TARGETS];
  702. if (!alloc_vda_req(a, &a->general_req)) {
  703. esas2r_hdebug(
  704. "failed to allocate a VDA request for the general req!");
  705. return false;
  706. }
  707. /* allocate requests for asynchronous events */
  708. a->first_ae_req =
  709. kzalloc(num_ae_requests * sizeof(struct esas2r_request),
  710. GFP_KERNEL);
  711. if (a->first_ae_req == NULL) {
  712. esas2r_log(ESAS2R_LOG_CRIT,
  713. "failed to allocate memory for asynchronous events");
  714. return false;
  715. }
  716. /* allocate the S/G list memory descriptors */
  717. a->sg_list_mds = kzalloc(
  718. num_sg_lists * sizeof(struct esas2r_mem_desc), GFP_KERNEL);
  719. if (a->sg_list_mds == NULL) {
  720. esas2r_log(ESAS2R_LOG_CRIT,
  721. "failed to allocate memory for s/g list descriptors");
  722. return false;
  723. }
  724. /* allocate the request table */
  725. a->req_table =
  726. kzalloc((num_requests + num_ae_requests +
  727. 1) * sizeof(struct esas2r_request *), GFP_KERNEL);
  728. if (a->req_table == NULL) {
  729. esas2r_log(ESAS2R_LOG_CRIT,
  730. "failed to allocate memory for the request table");
  731. return false;
  732. }
  733. /* initialize PCI configuration space */
  734. esas2r_init_pci_cfg_space(a);
  735. /*
  736. * the thunder_stream boards all have a serial flash part that has a
  737. * different base address on the AHB bus.
  738. */
  739. if ((a->pcid->subsystem_vendor == ATTO_VENDOR_ID)
  740. && (a->pcid->subsystem_device & ATTO_SSDID_TBT))
  741. a->flags2 |= AF2_THUNDERBOLT;
  742. if (test_bit(AF2_THUNDERBOLT, &a->flags2))
  743. a->flags2 |= AF2_SERIAL_FLASH;
  744. if (a->pcid->subsystem_device == ATTO_TLSH_1068)
  745. a->flags2 |= AF2_THUNDERLINK;
  746. /* Uncached Area */
  747. high = (u8 *)*uncached_area;
  748. /* initialize the scatter/gather table pages */
  749. for (i = 0, sgl = a->sg_list_mds; i < num_sg_lists; i++, sgl++) {
  750. sgl->size = sgl_page_size;
  751. list_add_tail(&sgl->next_desc, &a->free_sg_list_head);
  752. if (!esas2r_initmem_alloc(a, sgl, ESAS2R_SGL_ALIGN)) {
  753. /* Allow the driver to load if the minimum count met. */
  754. if (i < NUM_SGL_MIN)
  755. return false;
  756. break;
  757. }
  758. }
  759. /* compute the size of the lists */
  760. a->list_size = num_requests + ESAS2R_LIST_EXTRA;
  761. /* allocate the inbound list */
  762. a->inbound_list_md.size = a->list_size *
  763. sizeof(struct
  764. esas2r_inbound_list_source_entry);
  765. if (!esas2r_initmem_alloc(a, &a->inbound_list_md, ESAS2R_LIST_ALIGN)) {
  766. esas2r_hdebug("failed to allocate IB list");
  767. return false;
  768. }
  769. /* allocate the outbound list */
  770. a->outbound_list_md.size = a->list_size *
  771. sizeof(struct atto_vda_ob_rsp);
  772. if (!esas2r_initmem_alloc(a, &a->outbound_list_md,
  773. ESAS2R_LIST_ALIGN)) {
  774. esas2r_hdebug("failed to allocate IB list");
  775. return false;
  776. }
  777. /* allocate the NVRAM structure */
  778. a->nvram = (struct esas2r_sas_nvram *)high;
  779. high += sizeof(struct esas2r_sas_nvram);
  780. /* allocate the discovery buffer */
  781. a->disc_buffer = high;
  782. high += ESAS2R_DISC_BUF_LEN;
  783. high = PTR_ALIGN(high, 8);
  784. /* allocate the outbound list copy pointer */
  785. a->outbound_copy = (u32 volatile *)high;
  786. high += sizeof(u32);
  787. if (!test_bit(AF_NVR_VALID, &a->flags))
  788. esas2r_nvram_set_defaults(a);
  789. /* update the caller's uncached memory area pointer */
  790. *uncached_area = (void *)high;
  791. /* initialize the allocated memory */
  792. if (test_bit(AF_FIRST_INIT, &a->flags)) {
  793. esas2r_targ_db_initialize(a);
  794. /* prime parts of the inbound list */
  795. element =
  796. (struct esas2r_inbound_list_source_entry *)a->
  797. inbound_list_md.
  798. virt_addr;
  799. for (i = 0; i < a->list_size; i++) {
  800. element->address = 0;
  801. element->reserved = 0;
  802. element->length = cpu_to_le32(HWILSE_INTERFACE_F0
  803. | (sizeof(union
  804. atto_vda_req)
  805. /
  806. sizeof(u32)));
  807. element++;
  808. }
  809. /* init the AE requests */
  810. for (rq = a->first_ae_req, i = 0; i < num_ae_requests; rq++,
  811. i++) {
  812. INIT_LIST_HEAD(&rq->req_list);
  813. if (!alloc_vda_req(a, rq)) {
  814. esas2r_hdebug(
  815. "failed to allocate a VDA request!");
  816. return false;
  817. }
  818. esas2r_rq_init_request(rq, a);
  819. /* override the completion function */
  820. rq->comp_cb = esas2r_ae_complete;
  821. }
  822. }
  823. return true;
  824. }
  825. /* This code will verify that the chip is operational. */
  826. bool esas2r_check_adapter(struct esas2r_adapter *a)
  827. {
  828. u32 starttime;
  829. u32 doorbell;
  830. u64 ppaddr;
  831. u32 dw;
  832. /*
  833. * if the chip reset detected flag is set, we can bypass a bunch of
  834. * stuff.
  835. */
  836. if (test_bit(AF_CHPRST_DETECTED, &a->flags))
  837. goto skip_chip_reset;
  838. /*
  839. * BEFORE WE DO ANYTHING, disable the chip interrupts! the boot driver
  840. * may have left them enabled or we may be recovering from a fault.
  841. */
  842. esas2r_write_register_dword(a, MU_INT_MASK_OUT, ESAS2R_INT_DIS_MASK);
  843. esas2r_flush_register_dword(a, MU_INT_MASK_OUT);
  844. /*
  845. * wait for the firmware to become ready by forcing an interrupt and
  846. * waiting for a response.
  847. */
  848. starttime = jiffies_to_msecs(jiffies);
  849. while (true) {
  850. esas2r_force_interrupt(a);
  851. doorbell = esas2r_read_register_dword(a, MU_DOORBELL_OUT);
  852. if (doorbell == 0xFFFFFFFF) {
  853. /*
  854. * Give the firmware up to two seconds to enable
  855. * register access after a reset.
  856. */
  857. if ((jiffies_to_msecs(jiffies) - starttime) > 2000)
  858. return esas2r_set_degraded_mode(a,
  859. "unable to access registers");
  860. } else if (doorbell & DRBL_FORCE_INT) {
  861. u32 ver = (doorbell & DRBL_FW_VER_MSK);
  862. /*
  863. * This driver supports version 0 and version 1 of
  864. * the API
  865. */
  866. esas2r_write_register_dword(a, MU_DOORBELL_OUT,
  867. doorbell);
  868. if (ver == DRBL_FW_VER_0) {
  869. set_bit(AF_LEGACY_SGE_MODE, &a->flags);
  870. a->max_vdareq_size = 128;
  871. a->build_sgl = esas2r_build_sg_list_sge;
  872. } else if (ver == DRBL_FW_VER_1) {
  873. clear_bit(AF_LEGACY_SGE_MODE, &a->flags);
  874. a->max_vdareq_size = 1024;
  875. a->build_sgl = esas2r_build_sg_list_prd;
  876. } else {
  877. return esas2r_set_degraded_mode(a,
  878. "unknown firmware version");
  879. }
  880. break;
  881. }
  882. schedule_timeout_interruptible(msecs_to_jiffies(100));
  883. if ((jiffies_to_msecs(jiffies) - starttime) > 180000) {
  884. esas2r_hdebug("FW ready TMO");
  885. esas2r_bugon();
  886. return esas2r_set_degraded_mode(a,
  887. "firmware start has timed out");
  888. }
  889. }
  890. /* purge any asynchronous events since we will repost them later */
  891. esas2r_write_register_dword(a, MU_DOORBELL_IN, DRBL_MSG_IFC_DOWN);
  892. starttime = jiffies_to_msecs(jiffies);
  893. while (true) {
  894. doorbell = esas2r_read_register_dword(a, MU_DOORBELL_OUT);
  895. if (doorbell & DRBL_MSG_IFC_DOWN) {
  896. esas2r_write_register_dword(a, MU_DOORBELL_OUT,
  897. doorbell);
  898. break;
  899. }
  900. schedule_timeout_interruptible(msecs_to_jiffies(50));
  901. if ((jiffies_to_msecs(jiffies) - starttime) > 3000) {
  902. esas2r_hdebug("timeout waiting for interface down");
  903. break;
  904. }
  905. }
  906. skip_chip_reset:
  907. /*
  908. * first things first, before we go changing any of these registers
  909. * disable the communication lists.
  910. */
  911. dw = esas2r_read_register_dword(a, MU_IN_LIST_CONFIG);
  912. dw &= ~MU_ILC_ENABLE;
  913. esas2r_write_register_dword(a, MU_IN_LIST_CONFIG, dw);
  914. dw = esas2r_read_register_dword(a, MU_OUT_LIST_CONFIG);
  915. dw &= ~MU_OLC_ENABLE;
  916. esas2r_write_register_dword(a, MU_OUT_LIST_CONFIG, dw);
  917. /* configure the communication list addresses */
  918. ppaddr = a->inbound_list_md.phys_addr;
  919. esas2r_write_register_dword(a, MU_IN_LIST_ADDR_LO,
  920. lower_32_bits(ppaddr));
  921. esas2r_write_register_dword(a, MU_IN_LIST_ADDR_HI,
  922. upper_32_bits(ppaddr));
  923. ppaddr = a->outbound_list_md.phys_addr;
  924. esas2r_write_register_dword(a, MU_OUT_LIST_ADDR_LO,
  925. lower_32_bits(ppaddr));
  926. esas2r_write_register_dword(a, MU_OUT_LIST_ADDR_HI,
  927. upper_32_bits(ppaddr));
  928. ppaddr = a->uncached_phys +
  929. ((u8 *)a->outbound_copy - a->uncached);
  930. esas2r_write_register_dword(a, MU_OUT_LIST_COPY_PTR_LO,
  931. lower_32_bits(ppaddr));
  932. esas2r_write_register_dword(a, MU_OUT_LIST_COPY_PTR_HI,
  933. upper_32_bits(ppaddr));
  934. /* reset the read and write pointers */
  935. *a->outbound_copy =
  936. a->last_write =
  937. a->last_read = a->list_size - 1;
  938. set_bit(AF_COMM_LIST_TOGGLE, &a->flags);
  939. esas2r_write_register_dword(a, MU_IN_LIST_WRITE, MU_ILW_TOGGLE |
  940. a->last_write);
  941. esas2r_write_register_dword(a, MU_OUT_LIST_COPY, MU_OLC_TOGGLE |
  942. a->last_write);
  943. esas2r_write_register_dword(a, MU_IN_LIST_READ, MU_ILR_TOGGLE |
  944. a->last_write);
  945. esas2r_write_register_dword(a, MU_OUT_LIST_WRITE,
  946. MU_OLW_TOGGLE | a->last_write);
  947. /* configure the interface select fields */
  948. dw = esas2r_read_register_dword(a, MU_IN_LIST_IFC_CONFIG);
  949. dw &= ~(MU_ILIC_LIST | MU_ILIC_DEST);
  950. esas2r_write_register_dword(a, MU_IN_LIST_IFC_CONFIG,
  951. (dw | MU_ILIC_LIST_F0 | MU_ILIC_DEST_DDR));
  952. dw = esas2r_read_register_dword(a, MU_OUT_LIST_IFC_CONFIG);
  953. dw &= ~(MU_OLIC_LIST | MU_OLIC_SOURCE);
  954. esas2r_write_register_dword(a, MU_OUT_LIST_IFC_CONFIG,
  955. (dw | MU_OLIC_LIST_F0 |
  956. MU_OLIC_SOURCE_DDR));
  957. /* finish configuring the communication lists */
  958. dw = esas2r_read_register_dword(a, MU_IN_LIST_CONFIG);
  959. dw &= ~(MU_ILC_ENTRY_MASK | MU_ILC_NUMBER_MASK);
  960. dw |= MU_ILC_ENTRY_4_DW | MU_ILC_DYNAMIC_SRC
  961. | (a->list_size << MU_ILC_NUMBER_SHIFT);
  962. esas2r_write_register_dword(a, MU_IN_LIST_CONFIG, dw);
  963. dw = esas2r_read_register_dword(a, MU_OUT_LIST_CONFIG);
  964. dw &= ~(MU_OLC_ENTRY_MASK | MU_OLC_NUMBER_MASK);
  965. dw |= MU_OLC_ENTRY_4_DW | (a->list_size << MU_OLC_NUMBER_SHIFT);
  966. esas2r_write_register_dword(a, MU_OUT_LIST_CONFIG, dw);
  967. /*
  968. * notify the firmware that we're done setting up the communication
  969. * list registers. wait here until the firmware is done configuring
  970. * its lists. it will signal that it is done by enabling the lists.
  971. */
  972. esas2r_write_register_dword(a, MU_DOORBELL_IN, DRBL_MSG_IFC_INIT);
  973. starttime = jiffies_to_msecs(jiffies);
  974. while (true) {
  975. doorbell = esas2r_read_register_dword(a, MU_DOORBELL_OUT);
  976. if (doorbell & DRBL_MSG_IFC_INIT) {
  977. esas2r_write_register_dword(a, MU_DOORBELL_OUT,
  978. doorbell);
  979. break;
  980. }
  981. schedule_timeout_interruptible(msecs_to_jiffies(100));
  982. if ((jiffies_to_msecs(jiffies) - starttime) > 3000) {
  983. esas2r_hdebug(
  984. "timeout waiting for communication list init");
  985. esas2r_bugon();
  986. return esas2r_set_degraded_mode(a,
  987. "timeout waiting for communication list init");
  988. }
  989. }
  990. /*
  991. * flag whether the firmware supports the power down doorbell. we
  992. * determine this by reading the inbound doorbell enable mask.
  993. */
  994. doorbell = esas2r_read_register_dword(a, MU_DOORBELL_IN_ENB);
  995. if (doorbell & DRBL_POWER_DOWN)
  996. set_bit(AF2_VDA_POWER_DOWN, &a->flags2);
  997. else
  998. clear_bit(AF2_VDA_POWER_DOWN, &a->flags2);
  999. /*
  1000. * enable assertion of outbound queue and doorbell interrupts in the
  1001. * main interrupt cause register.
  1002. */
  1003. esas2r_write_register_dword(a, MU_OUT_LIST_INT_MASK, MU_OLIS_MASK);
  1004. esas2r_write_register_dword(a, MU_DOORBELL_OUT_ENB, DRBL_ENB_MASK);
  1005. return true;
  1006. }
  1007. /* Process the initialization message just completed and format the next one. */
  1008. static bool esas2r_format_init_msg(struct esas2r_adapter *a,
  1009. struct esas2r_request *rq)
  1010. {
  1011. u32 msg = a->init_msg;
  1012. struct atto_vda_cfg_init *ci;
  1013. a->init_msg = 0;
  1014. switch (msg) {
  1015. case ESAS2R_INIT_MSG_START:
  1016. case ESAS2R_INIT_MSG_REINIT:
  1017. {
  1018. esas2r_hdebug("CFG init");
  1019. esas2r_build_cfg_req(a,
  1020. rq,
  1021. VDA_CFG_INIT,
  1022. 0,
  1023. NULL);
  1024. ci = (struct atto_vda_cfg_init *)&rq->vrq->cfg.data.init;
  1025. ci->sgl_page_size = cpu_to_le32(sgl_page_size);
  1026. /* firmware interface overflows in y2106 */
  1027. ci->epoch_time = cpu_to_le32(ktime_get_real_seconds());
  1028. rq->flags |= RF_FAILURE_OK;
  1029. a->init_msg = ESAS2R_INIT_MSG_INIT;
  1030. break;
  1031. }
  1032. case ESAS2R_INIT_MSG_INIT:
  1033. if (rq->req_stat == RS_SUCCESS) {
  1034. u32 major;
  1035. u32 minor;
  1036. u16 fw_release;
  1037. a->fw_version = le16_to_cpu(
  1038. rq->func_rsp.cfg_rsp.vda_version);
  1039. a->fw_build = rq->func_rsp.cfg_rsp.fw_build;
  1040. fw_release = le16_to_cpu(
  1041. rq->func_rsp.cfg_rsp.fw_release);
  1042. major = LOBYTE(fw_release);
  1043. minor = HIBYTE(fw_release);
  1044. a->fw_version += (major << 16) + (minor << 24);
  1045. } else {
  1046. esas2r_hdebug("FAILED");
  1047. }
  1048. /*
  1049. * the 2.71 and earlier releases of R6xx firmware did not error
  1050. * unsupported config requests correctly.
  1051. */
  1052. if ((test_bit(AF2_THUNDERBOLT, &a->flags2))
  1053. || (be32_to_cpu(a->fw_version) > 0x00524702)) {
  1054. esas2r_hdebug("CFG get init");
  1055. esas2r_build_cfg_req(a,
  1056. rq,
  1057. VDA_CFG_GET_INIT2,
  1058. sizeof(struct atto_vda_cfg_init),
  1059. NULL);
  1060. rq->vrq->cfg.sg_list_offset = offsetof(
  1061. struct atto_vda_cfg_req,
  1062. data.sge);
  1063. rq->vrq->cfg.data.prde.ctl_len =
  1064. cpu_to_le32(sizeof(struct atto_vda_cfg_init));
  1065. rq->vrq->cfg.data.prde.address = cpu_to_le64(
  1066. rq->vrq_md->phys_addr +
  1067. sizeof(union atto_vda_req));
  1068. rq->flags |= RF_FAILURE_OK;
  1069. a->init_msg = ESAS2R_INIT_MSG_GET_INIT;
  1070. break;
  1071. }
  1072. case ESAS2R_INIT_MSG_GET_INIT:
  1073. if (msg == ESAS2R_INIT_MSG_GET_INIT) {
  1074. ci = (struct atto_vda_cfg_init *)rq->data_buf;
  1075. if (rq->req_stat == RS_SUCCESS) {
  1076. a->num_targets_backend =
  1077. le32_to_cpu(ci->num_targets_backend);
  1078. a->ioctl_tunnel =
  1079. le32_to_cpu(ci->ioctl_tunnel);
  1080. } else {
  1081. esas2r_hdebug("FAILED");
  1082. }
  1083. }
  1084. /* fall through */
  1085. default:
  1086. rq->req_stat = RS_SUCCESS;
  1087. return false;
  1088. }
  1089. return true;
  1090. }
  1091. /*
  1092. * Perform initialization messages via the request queue. Messages are
  1093. * performed with interrupts disabled.
  1094. */
  1095. bool esas2r_init_msgs(struct esas2r_adapter *a)
  1096. {
  1097. bool success = true;
  1098. struct esas2r_request *rq = &a->general_req;
  1099. esas2r_rq_init_request(rq, a);
  1100. rq->comp_cb = esas2r_dummy_complete;
  1101. if (a->init_msg == 0)
  1102. a->init_msg = ESAS2R_INIT_MSG_REINIT;
  1103. while (a->init_msg) {
  1104. if (esas2r_format_init_msg(a, rq)) {
  1105. unsigned long flags;
  1106. while (true) {
  1107. spin_lock_irqsave(&a->queue_lock, flags);
  1108. esas2r_start_vda_request(a, rq);
  1109. spin_unlock_irqrestore(&a->queue_lock, flags);
  1110. esas2r_wait_request(a, rq);
  1111. if (rq->req_stat != RS_PENDING)
  1112. break;
  1113. }
  1114. }
  1115. if (rq->req_stat == RS_SUCCESS
  1116. || ((rq->flags & RF_FAILURE_OK)
  1117. && rq->req_stat != RS_TIMEOUT))
  1118. continue;
  1119. esas2r_log(ESAS2R_LOG_CRIT, "init message %x failed (%x, %x)",
  1120. a->init_msg, rq->req_stat, rq->flags);
  1121. a->init_msg = ESAS2R_INIT_MSG_START;
  1122. success = false;
  1123. break;
  1124. }
  1125. esas2r_rq_destroy_request(rq, a);
  1126. return success;
  1127. }
  1128. /* Initialize the adapter chip */
  1129. bool esas2r_init_adapter_hw(struct esas2r_adapter *a, bool init_poll)
  1130. {
  1131. bool rslt = false;
  1132. struct esas2r_request *rq;
  1133. u32 i;
  1134. if (test_bit(AF_DEGRADED_MODE, &a->flags))
  1135. goto exit;
  1136. if (!test_bit(AF_NVR_VALID, &a->flags)) {
  1137. if (!esas2r_nvram_read_direct(a))
  1138. esas2r_log(ESAS2R_LOG_WARN,
  1139. "invalid/missing NVRAM parameters");
  1140. }
  1141. if (!esas2r_init_msgs(a)) {
  1142. esas2r_set_degraded_mode(a, "init messages failed");
  1143. goto exit;
  1144. }
  1145. /* The firmware is ready. */
  1146. clear_bit(AF_DEGRADED_MODE, &a->flags);
  1147. clear_bit(AF_CHPRST_PENDING, &a->flags);
  1148. /* Post all the async event requests */
  1149. for (i = 0, rq = a->first_ae_req; i < num_ae_requests; i++, rq++)
  1150. esas2r_start_ae_request(a, rq);
  1151. if (!a->flash_rev[0])
  1152. esas2r_read_flash_rev(a);
  1153. if (!a->image_type[0])
  1154. esas2r_read_image_type(a);
  1155. if (a->fw_version == 0)
  1156. a->fw_rev[0] = 0;
  1157. else
  1158. sprintf(a->fw_rev, "%1d.%02d",
  1159. (int)LOBYTE(HIWORD(a->fw_version)),
  1160. (int)HIBYTE(HIWORD(a->fw_version)));
  1161. esas2r_hdebug("firmware revision: %s", a->fw_rev);
  1162. if (test_bit(AF_CHPRST_DETECTED, &a->flags)
  1163. && (test_bit(AF_FIRST_INIT, &a->flags))) {
  1164. esas2r_enable_chip_interrupts(a);
  1165. return true;
  1166. }
  1167. /* initialize discovery */
  1168. esas2r_disc_initialize(a);
  1169. /*
  1170. * wait for the device wait time to expire here if requested. this is
  1171. * usually requested during initial driver load and possibly when
  1172. * resuming from a low power state. deferred device waiting will use
  1173. * interrupts. chip reset recovery always defers device waiting to
  1174. * avoid being in a TASKLET too long.
  1175. */
  1176. if (init_poll) {
  1177. u32 currtime = a->disc_start_time;
  1178. u32 nexttick = 100;
  1179. u32 deltatime;
  1180. /*
  1181. * Block Tasklets from getting scheduled and indicate this is
  1182. * polled discovery.
  1183. */
  1184. set_bit(AF_TASKLET_SCHEDULED, &a->flags);
  1185. set_bit(AF_DISC_POLLED, &a->flags);
  1186. /*
  1187. * Temporarily bring the disable count to zero to enable
  1188. * deferred processing. Note that the count is already zero
  1189. * after the first initialization.
  1190. */
  1191. if (test_bit(AF_FIRST_INIT, &a->flags))
  1192. atomic_dec(&a->disable_cnt);
  1193. while (test_bit(AF_DISC_PENDING, &a->flags)) {
  1194. schedule_timeout_interruptible(msecs_to_jiffies(100));
  1195. /*
  1196. * Determine the need for a timer tick based on the
  1197. * delta time between this and the last iteration of
  1198. * this loop. We don't use the absolute time because
  1199. * then we would have to worry about when nexttick
  1200. * wraps and currtime hasn't yet.
  1201. */
  1202. deltatime = jiffies_to_msecs(jiffies) - currtime;
  1203. currtime += deltatime;
  1204. /*
  1205. * Process any waiting discovery as long as the chip is
  1206. * up. If a chip reset happens during initial polling,
  1207. * we have to make sure the timer tick processes the
  1208. * doorbell indicating the firmware is ready.
  1209. */
  1210. if (!test_bit(AF_CHPRST_PENDING, &a->flags))
  1211. esas2r_disc_check_for_work(a);
  1212. /* Simulate a timer tick. */
  1213. if (nexttick <= deltatime) {
  1214. /* Time for a timer tick */
  1215. nexttick += 100;
  1216. esas2r_timer_tick(a);
  1217. }
  1218. if (nexttick > deltatime)
  1219. nexttick -= deltatime;
  1220. /* Do any deferred processing */
  1221. if (esas2r_is_tasklet_pending(a))
  1222. esas2r_do_tasklet_tasks(a);
  1223. }
  1224. if (test_bit(AF_FIRST_INIT, &a->flags))
  1225. atomic_inc(&a->disable_cnt);
  1226. clear_bit(AF_DISC_POLLED, &a->flags);
  1227. clear_bit(AF_TASKLET_SCHEDULED, &a->flags);
  1228. }
  1229. esas2r_targ_db_report_changes(a);
  1230. /*
  1231. * For cases where (a) the initialization messages processing may
  1232. * handle an interrupt for a port event and a discovery is waiting, but
  1233. * we are not waiting for devices, or (b) the device wait time has been
  1234. * exhausted but there is still discovery pending, start any leftover
  1235. * discovery in interrupt driven mode.
  1236. */
  1237. esas2r_disc_start_waiting(a);
  1238. /* Enable chip interrupts */
  1239. a->int_mask = ESAS2R_INT_STS_MASK;
  1240. esas2r_enable_chip_interrupts(a);
  1241. esas2r_enable_heartbeat(a);
  1242. rslt = true;
  1243. exit:
  1244. /*
  1245. * Regardless of whether initialization was successful, certain things
  1246. * need to get done before we exit.
  1247. */
  1248. if (test_bit(AF_CHPRST_DETECTED, &a->flags) &&
  1249. test_bit(AF_FIRST_INIT, &a->flags)) {
  1250. /*
  1251. * Reinitialization was performed during the first
  1252. * initialization. Only clear the chip reset flag so the
  1253. * original device polling is not cancelled.
  1254. */
  1255. if (!rslt)
  1256. clear_bit(AF_CHPRST_PENDING, &a->flags);
  1257. } else {
  1258. /* First initialization or a subsequent re-init is complete. */
  1259. if (!rslt) {
  1260. clear_bit(AF_CHPRST_PENDING, &a->flags);
  1261. clear_bit(AF_DISC_PENDING, &a->flags);
  1262. }
  1263. /* Enable deferred processing after the first initialization. */
  1264. if (test_bit(AF_FIRST_INIT, &a->flags)) {
  1265. clear_bit(AF_FIRST_INIT, &a->flags);
  1266. if (atomic_dec_return(&a->disable_cnt) == 0)
  1267. esas2r_do_deferred_processes(a);
  1268. }
  1269. }
  1270. return rslt;
  1271. }
  1272. void esas2r_reset_adapter(struct esas2r_adapter *a)
  1273. {
  1274. set_bit(AF_OS_RESET, &a->flags);
  1275. esas2r_local_reset_adapter(a);
  1276. esas2r_schedule_tasklet(a);
  1277. }
  1278. void esas2r_reset_chip(struct esas2r_adapter *a)
  1279. {
  1280. if (!esas2r_is_adapter_present(a))
  1281. return;
  1282. /*
  1283. * Before we reset the chip, save off the VDA core dump. The VDA core
  1284. * dump is located in the upper 512KB of the onchip SRAM. Make sure
  1285. * to not overwrite a previous crash that was saved.
  1286. */
  1287. if (test_bit(AF2_COREDUMP_AVAIL, &a->flags2) &&
  1288. !test_bit(AF2_COREDUMP_SAVED, &a->flags2)) {
  1289. esas2r_read_mem_block(a,
  1290. a->fw_coredump_buff,
  1291. MW_DATA_ADDR_SRAM + 0x80000,
  1292. ESAS2R_FWCOREDUMP_SZ);
  1293. set_bit(AF2_COREDUMP_SAVED, &a->flags2);
  1294. }
  1295. clear_bit(AF2_COREDUMP_AVAIL, &a->flags2);
  1296. /* Reset the chip */
  1297. if (a->pcid->revision == MVR_FREY_B2)
  1298. esas2r_write_register_dword(a, MU_CTL_STATUS_IN_B2,
  1299. MU_CTL_IN_FULL_RST2);
  1300. else
  1301. esas2r_write_register_dword(a, MU_CTL_STATUS_IN,
  1302. MU_CTL_IN_FULL_RST);
  1303. /* Stall a little while to let the reset condition clear */
  1304. mdelay(10);
  1305. }
  1306. static void esas2r_power_down_notify_firmware(struct esas2r_adapter *a)
  1307. {
  1308. u32 starttime;
  1309. u32 doorbell;
  1310. esas2r_write_register_dword(a, MU_DOORBELL_IN, DRBL_POWER_DOWN);
  1311. starttime = jiffies_to_msecs(jiffies);
  1312. while (true) {
  1313. doorbell = esas2r_read_register_dword(a, MU_DOORBELL_OUT);
  1314. if (doorbell & DRBL_POWER_DOWN) {
  1315. esas2r_write_register_dword(a, MU_DOORBELL_OUT,
  1316. doorbell);
  1317. break;
  1318. }
  1319. schedule_timeout_interruptible(msecs_to_jiffies(100));
  1320. if ((jiffies_to_msecs(jiffies) - starttime) > 30000) {
  1321. esas2r_hdebug("Timeout waiting for power down");
  1322. break;
  1323. }
  1324. }
  1325. }
  1326. /*
  1327. * Perform power management processing including managing device states, adapter
  1328. * states, interrupts, and I/O.
  1329. */
  1330. void esas2r_power_down(struct esas2r_adapter *a)
  1331. {
  1332. set_bit(AF_POWER_MGT, &a->flags);
  1333. set_bit(AF_POWER_DOWN, &a->flags);
  1334. if (!test_bit(AF_DEGRADED_MODE, &a->flags)) {
  1335. u32 starttime;
  1336. u32 doorbell;
  1337. /*
  1338. * We are currently running OK and will be reinitializing later.
  1339. * increment the disable count to coordinate with
  1340. * esas2r_init_adapter. We don't have to do this in degraded
  1341. * mode since we never enabled interrupts in the first place.
  1342. */
  1343. esas2r_disable_chip_interrupts(a);
  1344. esas2r_disable_heartbeat(a);
  1345. /* wait for any VDA activity to clear before continuing */
  1346. esas2r_write_register_dword(a, MU_DOORBELL_IN,
  1347. DRBL_MSG_IFC_DOWN);
  1348. starttime = jiffies_to_msecs(jiffies);
  1349. while (true) {
  1350. doorbell =
  1351. esas2r_read_register_dword(a, MU_DOORBELL_OUT);
  1352. if (doorbell & DRBL_MSG_IFC_DOWN) {
  1353. esas2r_write_register_dword(a, MU_DOORBELL_OUT,
  1354. doorbell);
  1355. break;
  1356. }
  1357. schedule_timeout_interruptible(msecs_to_jiffies(100));
  1358. if ((jiffies_to_msecs(jiffies) - starttime) > 3000) {
  1359. esas2r_hdebug(
  1360. "timeout waiting for interface down");
  1361. break;
  1362. }
  1363. }
  1364. /*
  1365. * For versions of firmware that support it tell them the driver
  1366. * is powering down.
  1367. */
  1368. if (test_bit(AF2_VDA_POWER_DOWN, &a->flags2))
  1369. esas2r_power_down_notify_firmware(a);
  1370. }
  1371. /* Suspend I/O processing. */
  1372. set_bit(AF_OS_RESET, &a->flags);
  1373. set_bit(AF_DISC_PENDING, &a->flags);
  1374. set_bit(AF_CHPRST_PENDING, &a->flags);
  1375. esas2r_process_adapter_reset(a);
  1376. /* Remove devices now that I/O is cleaned up. */
  1377. a->prev_dev_cnt = esas2r_targ_db_get_tgt_cnt(a);
  1378. esas2r_targ_db_remove_all(a, false);
  1379. }
  1380. /*
  1381. * Perform power management processing including managing device states, adapter
  1382. * states, interrupts, and I/O.
  1383. */
  1384. bool esas2r_power_up(struct esas2r_adapter *a, bool init_poll)
  1385. {
  1386. bool ret;
  1387. clear_bit(AF_POWER_DOWN, &a->flags);
  1388. esas2r_init_pci_cfg_space(a);
  1389. set_bit(AF_FIRST_INIT, &a->flags);
  1390. atomic_inc(&a->disable_cnt);
  1391. /* reinitialize the adapter */
  1392. ret = esas2r_check_adapter(a);
  1393. if (!esas2r_init_adapter_hw(a, init_poll))
  1394. ret = false;
  1395. /* send the reset asynchronous event */
  1396. esas2r_send_reset_ae(a, true);
  1397. /* clear this flag after initialization. */
  1398. clear_bit(AF_POWER_MGT, &a->flags);
  1399. return ret;
  1400. }
  1401. bool esas2r_is_adapter_present(struct esas2r_adapter *a)
  1402. {
  1403. if (test_bit(AF_NOT_PRESENT, &a->flags))
  1404. return false;
  1405. if (esas2r_read_register_dword(a, MU_DOORBELL_OUT) == 0xFFFFFFFF) {
  1406. set_bit(AF_NOT_PRESENT, &a->flags);
  1407. return false;
  1408. }
  1409. return true;
  1410. }
  1411. const char *esas2r_get_model_name(struct esas2r_adapter *a)
  1412. {
  1413. switch (a->pcid->subsystem_device) {
  1414. case ATTO_ESAS_R680:
  1415. return "ATTO ExpressSAS R680";
  1416. case ATTO_ESAS_R608:
  1417. return "ATTO ExpressSAS R608";
  1418. case ATTO_ESAS_R60F:
  1419. return "ATTO ExpressSAS R60F";
  1420. case ATTO_ESAS_R6F0:
  1421. return "ATTO ExpressSAS R6F0";
  1422. case ATTO_ESAS_R644:
  1423. return "ATTO ExpressSAS R644";
  1424. case ATTO_ESAS_R648:
  1425. return "ATTO ExpressSAS R648";
  1426. case ATTO_TSSC_3808:
  1427. return "ATTO ThunderStream SC 3808D";
  1428. case ATTO_TSSC_3808E:
  1429. return "ATTO ThunderStream SC 3808E";
  1430. case ATTO_TLSH_1068:
  1431. return "ATTO ThunderLink SH 1068";
  1432. }
  1433. return "ATTO SAS Controller";
  1434. }
  1435. const char *esas2r_get_model_name_short(struct esas2r_adapter *a)
  1436. {
  1437. switch (a->pcid->subsystem_device) {
  1438. case ATTO_ESAS_R680:
  1439. return "R680";
  1440. case ATTO_ESAS_R608:
  1441. return "R608";
  1442. case ATTO_ESAS_R60F:
  1443. return "R60F";
  1444. case ATTO_ESAS_R6F0:
  1445. return "R6F0";
  1446. case ATTO_ESAS_R644:
  1447. return "R644";
  1448. case ATTO_ESAS_R648:
  1449. return "R648";
  1450. case ATTO_TSSC_3808:
  1451. return "SC 3808D";
  1452. case ATTO_TSSC_3808E:
  1453. return "SC 3808E";
  1454. case ATTO_TLSH_1068:
  1455. return "SH 1068";
  1456. }
  1457. return "unknown";
  1458. }