mptspi.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471
  1. /*
  2. * linux/drivers/message/fusion/mptspi.c
  3. * For use with LSI Logic PCI chip/adapter(s)
  4. * running LSI Logic Fusion MPT (Message Passing Technology) firmware.
  5. *
  6. * Copyright (c) 1999-2005 LSI Logic Corporation
  7. * (mailto:mpt_linux_developer@lsil.com)
  8. *
  9. */
  10. /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
  11. /*
  12. This program is free software; you can redistribute it and/or modify
  13. it under the terms of the GNU General Public License as published by
  14. the Free Software Foundation; version 2 of the License.
  15. This program is distributed in the hope that it will be useful,
  16. but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. GNU General Public License for more details.
  19. NO WARRANTY
  20. THE PROGRAM IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OR
  21. CONDITIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED INCLUDING, WITHOUT
  22. LIMITATION, ANY WARRANTIES OR CONDITIONS OF TITLE, NON-INFRINGEMENT,
  23. MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. Each Recipient is
  24. solely responsible for determining the appropriateness of using and
  25. distributing the Program and assumes all risks associated with its
  26. exercise of rights under this Agreement, including but not limited to
  27. the risks and costs of program errors, damage to or loss of data,
  28. programs or equipment, and unavailability or interruption of operations.
  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. You should have received a copy of the GNU General Public License
  38. along with this program; if not, write to the Free Software
  39. Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  40. */
  41. /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
  42. #include "linux_compat.h" /* linux-2.6 tweaks */
  43. #include <linux/module.h>
  44. #include <linux/kernel.h>
  45. #include <linux/init.h>
  46. #include <linux/errno.h>
  47. #include <linux/kdev_t.h>
  48. #include <linux/blkdev.h>
  49. #include <linux/delay.h> /* for mdelay */
  50. #include <linux/interrupt.h> /* needed for in_interrupt() proto */
  51. #include <linux/reboot.h> /* notifier code */
  52. #include <linux/sched.h>
  53. #include <linux/workqueue.h>
  54. #include <scsi/scsi.h>
  55. #include <scsi/scsi_cmnd.h>
  56. #include <scsi/scsi_device.h>
  57. #include <scsi/scsi_host.h>
  58. #include <scsi/scsi_tcq.h>
  59. #include "mptbase.h"
  60. #include "mptscsih.h"
  61. /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
  62. #define my_NAME "Fusion MPT SPI Host driver"
  63. #define my_VERSION MPT_LINUX_VERSION_COMMON
  64. #define MYNAM "mptspi"
  65. MODULE_AUTHOR(MODULEAUTHOR);
  66. MODULE_DESCRIPTION(my_NAME);
  67. MODULE_LICENSE("GPL");
  68. /* Command line args */
  69. #ifdef MPTSCSIH_ENABLE_DOMAIN_VALIDATION
  70. static int mpt_dv = MPTSCSIH_DOMAIN_VALIDATION;
  71. module_param(mpt_dv, int, 0);
  72. MODULE_PARM_DESC(mpt_dv, " DV Algorithm: enhanced=1, basic=0 (default=MPTSCSIH_DOMAIN_VALIDATION=1)");
  73. static int mpt_width = MPTSCSIH_MAX_WIDTH;
  74. module_param(mpt_width, int, 0);
  75. MODULE_PARM_DESC(mpt_width, " Max Bus Width: wide=1, narrow=0 (default=MPTSCSIH_MAX_WIDTH=1)");
  76. static ushort mpt_factor = MPTSCSIH_MIN_SYNC;
  77. module_param(mpt_factor, ushort, 0);
  78. MODULE_PARM_DESC(mpt_factor, " Min Sync Factor (default=MPTSCSIH_MIN_SYNC=0x08)");
  79. #endif
  80. static int mpt_saf_te = MPTSCSIH_SAF_TE;
  81. module_param(mpt_saf_te, int, 0);
  82. MODULE_PARM_DESC(mpt_saf_te, " Force enabling SEP Processor: enable=1 (default=MPTSCSIH_SAF_TE=0)");
  83. static int mpt_pq_filter = 0;
  84. module_param(mpt_pq_filter, int, 0);
  85. MODULE_PARM_DESC(mpt_pq_filter, " Enable peripheral qualifier filter: enable=1 (default=0)");
  86. static int mptspiDoneCtx = -1;
  87. static int mptspiTaskCtx = -1;
  88. static int mptspiInternalCtx = -1; /* Used only for internal commands */
  89. static struct scsi_host_template mptspi_driver_template = {
  90. .module = THIS_MODULE,
  91. .proc_name = "mptspi",
  92. .proc_info = mptscsih_proc_info,
  93. .name = "MPT SPI Host",
  94. .info = mptscsih_info,
  95. .queuecommand = mptscsih_qcmd,
  96. .target_alloc = mptscsih_target_alloc,
  97. .slave_alloc = mptscsih_slave_alloc,
  98. .slave_configure = mptscsih_slave_configure,
  99. .target_destroy = mptscsih_target_destroy,
  100. .slave_destroy = mptscsih_slave_destroy,
  101. .change_queue_depth = mptscsih_change_queue_depth,
  102. .eh_abort_handler = mptscsih_abort,
  103. .eh_device_reset_handler = mptscsih_dev_reset,
  104. .eh_bus_reset_handler = mptscsih_bus_reset,
  105. .eh_host_reset_handler = mptscsih_host_reset,
  106. .bios_param = mptscsih_bios_param,
  107. .can_queue = MPT_SCSI_CAN_QUEUE,
  108. .this_id = -1,
  109. .sg_tablesize = MPT_SCSI_SG_DEPTH,
  110. .max_sectors = 8192,
  111. .cmd_per_lun = 7,
  112. .use_clustering = ENABLE_CLUSTERING,
  113. };
  114. /****************************************************************************
  115. * Supported hardware
  116. */
  117. static struct pci_device_id mptspi_pci_table[] = {
  118. { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_53C1030,
  119. PCI_ANY_ID, PCI_ANY_ID },
  120. { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_1030_53C1035,
  121. PCI_ANY_ID, PCI_ANY_ID },
  122. {0} /* Terminating entry */
  123. };
  124. MODULE_DEVICE_TABLE(pci, mptspi_pci_table);
  125. /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
  126. /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
  127. /*
  128. * mptspi_probe - Installs scsi devices per bus.
  129. * @pdev: Pointer to pci_dev structure
  130. *
  131. * Returns 0 for success, non-zero for failure.
  132. *
  133. */
  134. static int
  135. mptspi_probe(struct pci_dev *pdev, const struct pci_device_id *id)
  136. {
  137. struct Scsi_Host *sh;
  138. MPT_SCSI_HOST *hd;
  139. MPT_ADAPTER *ioc;
  140. unsigned long flags;
  141. int ii;
  142. int numSGE = 0;
  143. int scale;
  144. int ioc_cap;
  145. int error=0;
  146. int r;
  147. if ((r = mpt_attach(pdev,id)) != 0)
  148. return r;
  149. ioc = pci_get_drvdata(pdev);
  150. ioc->DoneCtx = mptspiDoneCtx;
  151. ioc->TaskCtx = mptspiTaskCtx;
  152. ioc->InternalCtx = mptspiInternalCtx;
  153. /* Added sanity check on readiness of the MPT adapter.
  154. */
  155. if (ioc->last_state != MPI_IOC_STATE_OPERATIONAL) {
  156. printk(MYIOC_s_WARN_FMT
  157. "Skipping because it's not operational!\n",
  158. ioc->name);
  159. error = -ENODEV;
  160. goto out_mptspi_probe;
  161. }
  162. if (!ioc->active) {
  163. printk(MYIOC_s_WARN_FMT "Skipping because it's disabled!\n",
  164. ioc->name);
  165. error = -ENODEV;
  166. goto out_mptspi_probe;
  167. }
  168. /* Sanity check - ensure at least 1 port is INITIATOR capable
  169. */
  170. ioc_cap = 0;
  171. for (ii=0; ii < ioc->facts.NumberOfPorts; ii++) {
  172. if (ioc->pfacts[ii].ProtocolFlags &
  173. MPI_PORTFACTS_PROTOCOL_INITIATOR)
  174. ioc_cap ++;
  175. }
  176. if (!ioc_cap) {
  177. printk(MYIOC_s_WARN_FMT
  178. "Skipping ioc=%p because SCSI Initiator mode is NOT enabled!\n",
  179. ioc->name, ioc);
  180. return 0;
  181. }
  182. sh = scsi_host_alloc(&mptspi_driver_template, sizeof(MPT_SCSI_HOST));
  183. if (!sh) {
  184. printk(MYIOC_s_WARN_FMT
  185. "Unable to register controller with SCSI subsystem\n",
  186. ioc->name);
  187. error = -1;
  188. goto out_mptspi_probe;
  189. }
  190. spin_lock_irqsave(&ioc->FreeQlock, flags);
  191. /* Attach the SCSI Host to the IOC structure
  192. */
  193. ioc->sh = sh;
  194. sh->io_port = 0;
  195. sh->n_io_port = 0;
  196. sh->irq = 0;
  197. /* set 16 byte cdb's */
  198. sh->max_cmd_len = 16;
  199. /* Yikes! This is important!
  200. * Otherwise, by default, linux
  201. * only scans target IDs 0-7!
  202. * pfactsN->MaxDevices unreliable
  203. * (not supported in early
  204. * versions of the FW).
  205. * max_id = 1 + actual max id,
  206. * max_lun = 1 + actual last lun,
  207. * see hosts.h :o(
  208. */
  209. sh->max_id = MPT_MAX_SCSI_DEVICES;
  210. sh->max_lun = MPT_LAST_LUN + 1;
  211. sh->max_channel = 0;
  212. sh->this_id = ioc->pfacts[0].PortSCSIID;
  213. /* Required entry.
  214. */
  215. sh->unique_id = ioc->id;
  216. /* Verify that we won't exceed the maximum
  217. * number of chain buffers
  218. * We can optimize: ZZ = req_sz/sizeof(SGE)
  219. * For 32bit SGE's:
  220. * numSGE = 1 + (ZZ-1)*(maxChain -1) + ZZ
  221. * + (req_sz - 64)/sizeof(SGE)
  222. * A slightly different algorithm is required for
  223. * 64bit SGEs.
  224. */
  225. scale = ioc->req_sz/(sizeof(dma_addr_t) + sizeof(u32));
  226. if (sizeof(dma_addr_t) == sizeof(u64)) {
  227. numSGE = (scale - 1) *
  228. (ioc->facts.MaxChainDepth-1) + scale +
  229. (ioc->req_sz - 60) / (sizeof(dma_addr_t) +
  230. sizeof(u32));
  231. } else {
  232. numSGE = 1 + (scale - 1) *
  233. (ioc->facts.MaxChainDepth-1) + scale +
  234. (ioc->req_sz - 64) / (sizeof(dma_addr_t) +
  235. sizeof(u32));
  236. }
  237. if (numSGE < sh->sg_tablesize) {
  238. /* Reset this value */
  239. dprintk((MYIOC_s_INFO_FMT
  240. "Resetting sg_tablesize to %d from %d\n",
  241. ioc->name, numSGE, sh->sg_tablesize));
  242. sh->sg_tablesize = numSGE;
  243. }
  244. spin_unlock_irqrestore(&ioc->FreeQlock, flags);
  245. hd = (MPT_SCSI_HOST *) sh->hostdata;
  246. hd->ioc = ioc;
  247. /* SCSI needs scsi_cmnd lookup table!
  248. * (with size equal to req_depth*PtrSz!)
  249. */
  250. hd->ScsiLookup = kcalloc(ioc->req_depth, sizeof(void *), GFP_ATOMIC);
  251. if (!hd->ScsiLookup) {
  252. error = -ENOMEM;
  253. goto out_mptspi_probe;
  254. }
  255. dprintk((MYIOC_s_INFO_FMT "ScsiLookup @ %p\n",
  256. ioc->name, hd->ScsiLookup));
  257. /* Allocate memory for the device structures.
  258. * A non-Null pointer at an offset
  259. * indicates a device exists.
  260. * max_id = 1 + maximum id (hosts.h)
  261. */
  262. hd->Targets = kcalloc(sh->max_id, sizeof(void *), GFP_ATOMIC);
  263. if (!hd->Targets) {
  264. error = -ENOMEM;
  265. goto out_mptspi_probe;
  266. }
  267. dprintk((KERN_INFO " vdev @ %p\n", hd->Targets));
  268. /* Clear the TM flags
  269. */
  270. hd->tmPending = 0;
  271. hd->tmState = TM_STATE_NONE;
  272. hd->resetPending = 0;
  273. hd->abortSCpnt = NULL;
  274. /* Clear the pointer used to store
  275. * single-threaded commands, i.e., those
  276. * issued during a bus scan, dv and
  277. * configuration pages.
  278. */
  279. hd->cmdPtr = NULL;
  280. /* Initialize this SCSI Hosts' timers
  281. * To use, set the timer expires field
  282. * and add_timer
  283. */
  284. init_timer(&hd->timer);
  285. hd->timer.data = (unsigned long) hd;
  286. hd->timer.function = mptscsih_timer_expired;
  287. ioc->spi_data.Saf_Te = mpt_saf_te;
  288. hd->mpt_pq_filter = mpt_pq_filter;
  289. #ifdef MPTSCSIH_ENABLE_DOMAIN_VALIDATION
  290. if (ioc->spi_data.maxBusWidth > mpt_width)
  291. ioc->spi_data.maxBusWidth = mpt_width;
  292. if (ioc->spi_data.minSyncFactor < mpt_factor)
  293. ioc->spi_data.minSyncFactor = mpt_factor;
  294. if (ioc->spi_data.minSyncFactor == MPT_ASYNC) {
  295. ioc->spi_data.maxSyncOffset = 0;
  296. }
  297. ioc->spi_data.mpt_dv = mpt_dv;
  298. hd->negoNvram = 0;
  299. ddvprintk((MYIOC_s_INFO_FMT
  300. "dv %x width %x factor %x saf_te %x mpt_pq_filter %x\n",
  301. ioc->name,
  302. mpt_dv,
  303. mpt_width,
  304. mpt_factor,
  305. mpt_saf_te,
  306. mpt_pq_filter));
  307. #else
  308. hd->negoNvram = MPT_SCSICFG_USE_NVRAM;
  309. ddvprintk((MYIOC_s_INFO_FMT
  310. "saf_te %x mpt_pq_filter %x\n",
  311. ioc->name,
  312. mpt_saf_te,
  313. mpt_pq_filter));
  314. #endif
  315. ioc->spi_data.forceDv = 0;
  316. ioc->spi_data.noQas = 0;
  317. for (ii=0; ii < MPT_MAX_SCSI_DEVICES; ii++)
  318. ioc->spi_data.dvStatus[ii] =
  319. MPT_SCSICFG_NEGOTIATE;
  320. for (ii=0; ii < MPT_MAX_SCSI_DEVICES; ii++)
  321. ioc->spi_data.dvStatus[ii] |=
  322. MPT_SCSICFG_DV_NOT_DONE;
  323. init_waitqueue_head(&hd->scandv_waitq);
  324. hd->scandv_wait_done = 0;
  325. hd->last_queue_full = 0;
  326. error = scsi_add_host (sh, &ioc->pcidev->dev);
  327. if(error) {
  328. dprintk((KERN_ERR MYNAM
  329. "scsi_add_host failed\n"));
  330. goto out_mptspi_probe;
  331. }
  332. /*
  333. * issue internal bus reset
  334. */
  335. if (ioc->spi_data.bus_reset)
  336. mptscsih_TMHandler(hd,
  337. MPI_SCSITASKMGMT_TASKTYPE_RESET_BUS,
  338. 0, 0, 0, 0, 5);
  339. scsi_scan_host(sh);
  340. return 0;
  341. out_mptspi_probe:
  342. mptscsih_remove(pdev);
  343. return error;
  344. }
  345. static struct pci_driver mptspi_driver = {
  346. .name = "mptspi",
  347. .id_table = mptspi_pci_table,
  348. .probe = mptspi_probe,
  349. .remove = __devexit_p(mptscsih_remove),
  350. .shutdown = mptscsih_shutdown,
  351. #ifdef CONFIG_PM
  352. .suspend = mptscsih_suspend,
  353. .resume = mptscsih_resume,
  354. #endif
  355. };
  356. /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
  357. /**
  358. * mptspi_init - Register MPT adapter(s) as SCSI host(s) with
  359. * linux scsi mid-layer.
  360. *
  361. * Returns 0 for success, non-zero for failure.
  362. */
  363. static int __init
  364. mptspi_init(void)
  365. {
  366. show_mptmod_ver(my_NAME, my_VERSION);
  367. mptspiDoneCtx = mpt_register(mptscsih_io_done, MPTSPI_DRIVER);
  368. mptspiTaskCtx = mpt_register(mptscsih_taskmgmt_complete, MPTSPI_DRIVER);
  369. mptspiInternalCtx = mpt_register(mptscsih_scandv_complete, MPTSPI_DRIVER);
  370. if (mpt_event_register(mptspiDoneCtx, mptscsih_event_process) == 0) {
  371. devtprintk((KERN_INFO MYNAM
  372. ": Registered for IOC event notifications\n"));
  373. }
  374. if (mpt_reset_register(mptspiDoneCtx, mptscsih_ioc_reset) == 0) {
  375. dprintk((KERN_INFO MYNAM
  376. ": Registered for IOC reset notifications\n"));
  377. }
  378. return pci_register_driver(&mptspi_driver);
  379. }
  380. /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
  381. /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
  382. /**
  383. * mptspi_exit - Unregisters MPT adapter(s)
  384. *
  385. */
  386. static void __exit
  387. mptspi_exit(void)
  388. {
  389. pci_unregister_driver(&mptspi_driver);
  390. mpt_reset_deregister(mptspiDoneCtx);
  391. dprintk((KERN_INFO MYNAM
  392. ": Deregistered for IOC reset notifications\n"));
  393. mpt_event_deregister(mptspiDoneCtx);
  394. dprintk((KERN_INFO MYNAM
  395. ": Deregistered for IOC event notifications\n"));
  396. mpt_deregister(mptspiInternalCtx);
  397. mpt_deregister(mptspiTaskCtx);
  398. mpt_deregister(mptspiDoneCtx);
  399. }
  400. module_init(mptspi_init);
  401. module_exit(mptspi_exit);