mptfc.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929
  1. /*
  2. * linux/drivers/message/fusion/mptfc.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 <linux/sort.h>
  55. #include <scsi/scsi.h>
  56. #include <scsi/scsi_cmnd.h>
  57. #include <scsi/scsi_device.h>
  58. #include <scsi/scsi_host.h>
  59. #include <scsi/scsi_tcq.h>
  60. #include <scsi/scsi_transport_fc.h>
  61. #include "mptbase.h"
  62. #include "mptscsih.h"
  63. /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
  64. #define my_NAME "Fusion MPT FC Host driver"
  65. #define my_VERSION MPT_LINUX_VERSION_COMMON
  66. #define MYNAM "mptfc"
  67. MODULE_AUTHOR(MODULEAUTHOR);
  68. MODULE_DESCRIPTION(my_NAME);
  69. MODULE_LICENSE("GPL");
  70. /* Command line args */
  71. static int mpt_pq_filter = 0;
  72. module_param(mpt_pq_filter, int, 0);
  73. MODULE_PARM_DESC(mpt_pq_filter, " Enable peripheral qualifier filter: enable=1 (default=0)");
  74. #define MPTFC_DEV_LOSS_TMO (60)
  75. static int mptfc_dev_loss_tmo = MPTFC_DEV_LOSS_TMO; /* reasonable default */
  76. module_param(mptfc_dev_loss_tmo, int, 0);
  77. MODULE_PARM_DESC(mptfc_dev_loss_tmo, " Initial time the driver programs the "
  78. " transport to wait for an rport to "
  79. " return following a device loss event."
  80. " Default=60.");
  81. static int mptfcDoneCtx = -1;
  82. static int mptfcTaskCtx = -1;
  83. static int mptfcInternalCtx = -1; /* Used only for internal commands */
  84. int mptfc_slave_alloc(struct scsi_device *device);
  85. static int mptfc_qcmd(struct scsi_cmnd *SCpnt,
  86. void (*done)(struct scsi_cmnd *));
  87. static void mptfc_set_rport_loss_tmo(struct fc_rport *rport, uint32_t timeout);
  88. static void __devexit mptfc_remove(struct pci_dev *pdev);
  89. static struct scsi_host_template mptfc_driver_template = {
  90. .module = THIS_MODULE,
  91. .proc_name = "mptfc",
  92. .proc_info = mptscsih_proc_info,
  93. .name = "MPT FC Host",
  94. .info = mptscsih_info,
  95. .queuecommand = mptfc_qcmd,
  96. .target_alloc = mptscsih_target_alloc,
  97. .slave_alloc = mptfc_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_FC_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 mptfc_pci_table[] = {
  118. { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_FC909,
  119. PCI_ANY_ID, PCI_ANY_ID },
  120. { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_FC919,
  121. PCI_ANY_ID, PCI_ANY_ID },
  122. { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_FC929,
  123. PCI_ANY_ID, PCI_ANY_ID },
  124. { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_FC919X,
  125. PCI_ANY_ID, PCI_ANY_ID },
  126. { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_FC929X,
  127. PCI_ANY_ID, PCI_ANY_ID },
  128. { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_FC939X,
  129. PCI_ANY_ID, PCI_ANY_ID },
  130. { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_FC949X,
  131. PCI_ANY_ID, PCI_ANY_ID },
  132. { PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_FC949ES,
  133. PCI_ANY_ID, PCI_ANY_ID },
  134. {0} /* Terminating entry */
  135. };
  136. MODULE_DEVICE_TABLE(pci, mptfc_pci_table);
  137. static struct scsi_transport_template *mptfc_transport_template = NULL;
  138. struct fc_function_template mptfc_transport_functions = {
  139. .dd_fcrport_size = 8,
  140. .show_host_node_name = 1,
  141. .show_host_port_name = 1,
  142. .show_host_supported_classes = 1,
  143. .show_host_port_id = 1,
  144. .show_rport_supported_classes = 1,
  145. .show_starget_node_name = 1,
  146. .show_starget_port_name = 1,
  147. .show_starget_port_id = 1,
  148. .set_rport_dev_loss_tmo = mptfc_set_rport_loss_tmo,
  149. .show_rport_dev_loss_tmo = 1,
  150. };
  151. /* FIXME! values controlling firmware RESCAN event
  152. * need to be set low to allow dev_loss_tmo to
  153. * work as expected. Currently, firmware doesn't
  154. * notify driver of RESCAN event until some number
  155. * of seconds elapse. This value can be set via
  156. * lsiutil.
  157. */
  158. static void
  159. mptfc_set_rport_loss_tmo(struct fc_rport *rport, uint32_t timeout)
  160. {
  161. if (timeout > 0)
  162. rport->dev_loss_tmo = timeout;
  163. else
  164. rport->dev_loss_tmo = mptfc_dev_loss_tmo;
  165. }
  166. static int
  167. mptfc_FcDevPage0_cmp_func(const void *a, const void *b)
  168. {
  169. FCDevicePage0_t **aa = (FCDevicePage0_t **)a;
  170. FCDevicePage0_t **bb = (FCDevicePage0_t **)b;
  171. if ((*aa)->CurrentBus == (*bb)->CurrentBus) {
  172. if ((*aa)->CurrentTargetID == (*bb)->CurrentTargetID)
  173. return 0;
  174. if ((*aa)->CurrentTargetID < (*bb)->CurrentTargetID)
  175. return -1;
  176. return 1;
  177. }
  178. if ((*aa)->CurrentBus < (*bb)->CurrentBus)
  179. return -1;
  180. return 1;
  181. }
  182. static int
  183. mptfc_GetFcDevPage0(MPT_ADAPTER *ioc, int ioc_port,
  184. void(*func)(MPT_ADAPTER *ioc,int channel, FCDevicePage0_t *arg))
  185. {
  186. ConfigPageHeader_t hdr;
  187. CONFIGPARMS cfg;
  188. FCDevicePage0_t *ppage0_alloc, *fc;
  189. dma_addr_t page0_dma;
  190. int data_sz;
  191. int ii;
  192. FCDevicePage0_t *p0_array=NULL, *p_p0;
  193. FCDevicePage0_t **pp0_array=NULL, **p_pp0;
  194. int rc = -ENOMEM;
  195. U32 port_id = 0xffffff;
  196. int num_targ = 0;
  197. int max_bus = ioc->facts.MaxBuses;
  198. int max_targ = ioc->facts.MaxDevices;
  199. if (max_bus == 0 || max_targ == 0)
  200. goto out;
  201. data_sz = sizeof(FCDevicePage0_t) * max_bus * max_targ;
  202. p_p0 = p0_array = kzalloc(data_sz, GFP_KERNEL);
  203. if (!p0_array)
  204. goto out;
  205. data_sz = sizeof(FCDevicePage0_t *) * max_bus * max_targ;
  206. p_pp0 = pp0_array = kzalloc(data_sz, GFP_KERNEL);
  207. if (!pp0_array)
  208. goto out;
  209. do {
  210. /* Get FC Device Page 0 header */
  211. hdr.PageVersion = 0;
  212. hdr.PageLength = 0;
  213. hdr.PageNumber = 0;
  214. hdr.PageType = MPI_CONFIG_PAGETYPE_FC_DEVICE;
  215. cfg.cfghdr.hdr = &hdr;
  216. cfg.physAddr = -1;
  217. cfg.action = MPI_CONFIG_ACTION_PAGE_HEADER;
  218. cfg.dir = 0;
  219. cfg.pageAddr = port_id;
  220. cfg.timeout = 0;
  221. if ((rc = mpt_config(ioc, &cfg)) != 0)
  222. break;
  223. if (hdr.PageLength <= 0)
  224. break;
  225. data_sz = hdr.PageLength * 4;
  226. ppage0_alloc = pci_alloc_consistent(ioc->pcidev, data_sz,
  227. &page0_dma);
  228. rc = -ENOMEM;
  229. if (!ppage0_alloc)
  230. break;
  231. cfg.physAddr = page0_dma;
  232. cfg.action = MPI_CONFIG_ACTION_PAGE_READ_CURRENT;
  233. if ((rc = mpt_config(ioc, &cfg)) == 0) {
  234. ppage0_alloc->PortIdentifier =
  235. le32_to_cpu(ppage0_alloc->PortIdentifier);
  236. ppage0_alloc->WWNN.Low =
  237. le32_to_cpu(ppage0_alloc->WWNN.Low);
  238. ppage0_alloc->WWNN.High =
  239. le32_to_cpu(ppage0_alloc->WWNN.High);
  240. ppage0_alloc->WWPN.Low =
  241. le32_to_cpu(ppage0_alloc->WWPN.Low);
  242. ppage0_alloc->WWPN.High =
  243. le32_to_cpu(ppage0_alloc->WWPN.High);
  244. ppage0_alloc->BBCredit =
  245. le16_to_cpu(ppage0_alloc->BBCredit);
  246. ppage0_alloc->MaxRxFrameSize =
  247. le16_to_cpu(ppage0_alloc->MaxRxFrameSize);
  248. port_id = ppage0_alloc->PortIdentifier;
  249. num_targ++;
  250. *p_p0 = *ppage0_alloc; /* save data */
  251. *p_pp0++ = p_p0++; /* save addr */
  252. }
  253. pci_free_consistent(ioc->pcidev, data_sz,
  254. (u8 *) ppage0_alloc, page0_dma);
  255. if (rc != 0)
  256. break;
  257. } while (port_id <= 0xff0000);
  258. if (num_targ) {
  259. /* sort array */
  260. if (num_targ > 1)
  261. sort (pp0_array, num_targ, sizeof(FCDevicePage0_t *),
  262. mptfc_FcDevPage0_cmp_func, NULL);
  263. /* call caller's func for each targ */
  264. for (ii = 0; ii < num_targ; ii++) {
  265. fc = *(pp0_array+ii);
  266. func(ioc, ioc_port, fc);
  267. }
  268. }
  269. out:
  270. if (pp0_array)
  271. kfree(pp0_array);
  272. if (p0_array)
  273. kfree(p0_array);
  274. return rc;
  275. }
  276. static int
  277. mptfc_generate_rport_ids(FCDevicePage0_t *pg0, struct fc_rport_identifiers *rid)
  278. {
  279. /* not currently usable */
  280. if (pg0->Flags & (MPI_FC_DEVICE_PAGE0_FLAGS_PLOGI_INVALID |
  281. MPI_FC_DEVICE_PAGE0_FLAGS_PRLI_INVALID))
  282. return -1;
  283. if (!(pg0->Flags & MPI_FC_DEVICE_PAGE0_FLAGS_TARGETID_BUS_VALID))
  284. return -1;
  285. if (!(pg0->Protocol & MPI_FC_DEVICE_PAGE0_PROT_FCP_TARGET))
  286. return -1;
  287. /*
  288. * board data structure already normalized to platform endianness
  289. * shifted to avoid unaligned access on 64 bit architecture
  290. */
  291. rid->node_name = ((u64)pg0->WWNN.High) << 32 | (u64)pg0->WWNN.Low;
  292. rid->port_name = ((u64)pg0->WWPN.High) << 32 | (u64)pg0->WWPN.Low;
  293. rid->port_id = pg0->PortIdentifier;
  294. rid->roles = FC_RPORT_ROLE_UNKNOWN;
  295. rid->roles |= FC_RPORT_ROLE_FCP_TARGET;
  296. if (pg0->Protocol & MPI_FC_DEVICE_PAGE0_PROT_FCP_INITIATOR)
  297. rid->roles |= FC_RPORT_ROLE_FCP_INITIATOR;
  298. return 0;
  299. }
  300. static void
  301. mptfc_register_dev(MPT_ADAPTER *ioc, int channel, FCDevicePage0_t *pg0)
  302. {
  303. struct fc_rport_identifiers rport_ids;
  304. struct fc_rport *rport;
  305. struct mptfc_rport_info *ri;
  306. int match = 0;
  307. u64 port_name;
  308. unsigned long flags;
  309. if (mptfc_generate_rport_ids(pg0, &rport_ids) < 0)
  310. return;
  311. /* scan list looking for a match */
  312. spin_lock_irqsave(&ioc->fc_rport_lock, flags);
  313. list_for_each_entry(ri, &ioc->fc_rports, list) {
  314. port_name = (u64)ri->pg0.WWPN.High << 32 | (u64)ri->pg0.WWPN.Low;
  315. if (port_name == rport_ids.port_name) { /* match */
  316. list_move_tail(&ri->list, &ioc->fc_rports);
  317. match = 1;
  318. break;
  319. }
  320. }
  321. if (!match) { /* allocate one */
  322. spin_unlock_irqrestore(&ioc->fc_rport_lock, flags);
  323. ri = kzalloc(sizeof(struct mptfc_rport_info), GFP_KERNEL);
  324. if (!ri)
  325. return;
  326. spin_lock_irqsave(&ioc->fc_rport_lock, flags);
  327. list_add_tail(&ri->list, &ioc->fc_rports);
  328. }
  329. ri->pg0 = *pg0; /* add/update pg0 data */
  330. ri->flags &= ~MPT_RPORT_INFO_FLAGS_MISSING;
  331. if (!(ri->flags & MPT_RPORT_INFO_FLAGS_REGISTERED)) {
  332. ri->flags |= MPT_RPORT_INFO_FLAGS_REGISTERED;
  333. spin_unlock_irqrestore(&ioc->fc_rport_lock, flags);
  334. rport = fc_remote_port_add(ioc->sh,channel, &rport_ids);
  335. spin_lock_irqsave(&ioc->fc_rport_lock, flags);
  336. if (rport) {
  337. if (*((struct mptfc_rport_info **)rport->dd_data) != ri) {
  338. ri->flags &= ~MPT_RPORT_INFO_FLAGS_MAPPED_VDEV;
  339. ri->vdev = NULL;
  340. ri->rport = rport;
  341. *((struct mptfc_rport_info **)rport->dd_data) = ri;
  342. }
  343. rport->dev_loss_tmo = mptfc_dev_loss_tmo;
  344. /*
  345. * if already mapped, remap here. If not mapped,
  346. * slave_alloc will allocate vdev and map
  347. */
  348. if (ri->flags & MPT_RPORT_INFO_FLAGS_MAPPED_VDEV) {
  349. ri->vdev->target_id = ri->pg0.CurrentTargetID;
  350. ri->vdev->bus_id = ri->pg0.CurrentBus;
  351. ri->vdev->vtarget->target_id = ri->vdev->target_id;
  352. ri->vdev->vtarget->bus_id = ri->vdev->bus_id;
  353. }
  354. #ifdef MPT_DEBUG
  355. printk ("mptfc_reg_dev.%d: %x, %llx / %llx, tid %d, "
  356. "rport tid %d, tmo %d\n",
  357. ioc->sh->host_no,
  358. pg0->PortIdentifier,
  359. pg0->WWNN,
  360. pg0->WWPN,
  361. pg0->CurrentTargetID,
  362. ri->rport->scsi_target_id,
  363. ri->rport->dev_loss_tmo);
  364. #endif
  365. } else {
  366. list_del(&ri->list);
  367. kfree(ri);
  368. ri = NULL;
  369. }
  370. }
  371. spin_unlock_irqrestore(&ioc->fc_rport_lock,flags);
  372. }
  373. /*
  374. * OS entry point to allow host driver to alloc memory
  375. * for each scsi device. Called once per device the bus scan.
  376. * Return non-zero if allocation fails.
  377. * Init memory once per LUN.
  378. */
  379. int
  380. mptfc_slave_alloc(struct scsi_device *sdev)
  381. {
  382. MPT_SCSI_HOST *hd;
  383. VirtTarget *vtarget;
  384. VirtDevice *vdev;
  385. struct scsi_target *starget;
  386. struct fc_rport *rport;
  387. struct mptfc_rport_info *ri;
  388. unsigned long flags;
  389. rport = starget_to_rport(scsi_target(sdev));
  390. if (!rport || fc_remote_port_chkready(rport))
  391. return -ENXIO;
  392. hd = (MPT_SCSI_HOST *)sdev->host->hostdata;
  393. vdev = kmalloc(sizeof(VirtDevice), GFP_KERNEL);
  394. if (!vdev) {
  395. printk(MYIOC_s_ERR_FMT "slave_alloc kmalloc(%zd) FAILED!\n",
  396. hd->ioc->name, sizeof(VirtDevice));
  397. return -ENOMEM;
  398. }
  399. memset(vdev, 0, sizeof(VirtDevice));
  400. spin_lock_irqsave(&hd->ioc->fc_rport_lock,flags);
  401. if (!(ri = *((struct mptfc_rport_info **)rport->dd_data))) {
  402. spin_unlock_irqrestore(&hd->ioc->fc_rport_lock,flags);
  403. kfree(vdev);
  404. return -ENODEV;
  405. }
  406. sdev->hostdata = vdev;
  407. starget = scsi_target(sdev);
  408. vtarget = starget->hostdata;
  409. if (vtarget->num_luns == 0) {
  410. vtarget->tflags = MPT_TARGET_FLAGS_Q_YES |
  411. MPT_TARGET_FLAGS_VALID_INQUIRY;
  412. hd->Targets[sdev->id] = vtarget;
  413. }
  414. vtarget->target_id = vdev->target_id;
  415. vtarget->bus_id = vdev->bus_id;
  416. vdev->vtarget = vtarget;
  417. vdev->ioc_id = hd->ioc->id;
  418. vdev->lun = sdev->lun;
  419. vdev->target_id = ri->pg0.CurrentTargetID;
  420. vdev->bus_id = ri->pg0.CurrentBus;
  421. ri->flags |= MPT_RPORT_INFO_FLAGS_MAPPED_VDEV;
  422. ri->vdev = vdev;
  423. spin_unlock_irqrestore(&hd->ioc->fc_rport_lock,flags);
  424. vtarget->num_luns++;
  425. #ifdef MPT_DEBUG
  426. printk ("mptfc_slv_alloc.%d: num_luns %d, sdev.id %d, "
  427. "CurrentTargetID %d, %x %llx %llx\n",
  428. sdev->host->host_no,
  429. vtarget->num_luns,
  430. sdev->id, ri->pg0.CurrentTargetID,
  431. ri->pg0.PortIdentifier, ri->pg0.WWPN, ri->pg0.WWNN);
  432. #endif
  433. return 0;
  434. }
  435. static int
  436. mptfc_qcmd(struct scsi_cmnd *SCpnt, void (*done)(struct scsi_cmnd *))
  437. {
  438. struct fc_rport *rport = starget_to_rport(scsi_target(SCpnt->device));
  439. int err;
  440. err = fc_remote_port_chkready(rport);
  441. if (unlikely(err)) {
  442. SCpnt->result = err;
  443. done(SCpnt);
  444. return 0;
  445. }
  446. return mptscsih_qcmd(SCpnt,done);
  447. }
  448. static void
  449. mptfc_init_host_attr(MPT_ADAPTER *ioc,int portnum)
  450. {
  451. unsigned class = 0, cos = 0;
  452. /* don't know what to do as only one scsi (fc) host was allocated */
  453. if (portnum != 0)
  454. return;
  455. class = ioc->fc_port_page0[portnum].SupportedServiceClass;
  456. if (class & MPI_FCPORTPAGE0_SUPPORT_CLASS_1)
  457. cos |= FC_COS_CLASS1;
  458. if (class & MPI_FCPORTPAGE0_SUPPORT_CLASS_2)
  459. cos |= FC_COS_CLASS2;
  460. if (class & MPI_FCPORTPAGE0_SUPPORT_CLASS_3)
  461. cos |= FC_COS_CLASS3;
  462. fc_host_node_name(ioc->sh) =
  463. (u64)ioc->fc_port_page0[portnum].WWNN.High << 32
  464. | (u64)ioc->fc_port_page0[portnum].WWNN.Low;
  465. fc_host_port_name(ioc->sh) =
  466. (u64)ioc->fc_port_page0[portnum].WWPN.High << 32
  467. | (u64)ioc->fc_port_page0[portnum].WWPN.Low;
  468. fc_host_port_id(ioc->sh) = ioc->fc_port_page0[portnum].PortIdentifier;
  469. fc_host_supported_classes(ioc->sh) = cos;
  470. fc_host_tgtid_bind_type(ioc->sh) = FC_TGTID_BIND_BY_WWPN;
  471. }
  472. static void
  473. mptfc_rescan_devices(void *arg)
  474. {
  475. MPT_ADAPTER *ioc = (MPT_ADAPTER *)arg;
  476. int ii;
  477. int work_to_do;
  478. unsigned long flags;
  479. struct mptfc_rport_info *ri;
  480. do {
  481. /* start by tagging all ports as missing */
  482. spin_lock_irqsave(&ioc->fc_rport_lock,flags);
  483. list_for_each_entry(ri, &ioc->fc_rports, list) {
  484. if (ri->flags & MPT_RPORT_INFO_FLAGS_REGISTERED) {
  485. ri->flags |= MPT_RPORT_INFO_FLAGS_MISSING;
  486. }
  487. }
  488. spin_unlock_irqrestore(&ioc->fc_rport_lock,flags);
  489. /*
  490. * now rescan devices known to adapter,
  491. * will reregister existing rports
  492. */
  493. for (ii=0; ii < ioc->facts.NumberOfPorts; ii++) {
  494. (void) mptbase_GetFcPortPage0(ioc, ii);
  495. mptfc_init_host_attr(ioc,ii); /* refresh */
  496. mptfc_GetFcDevPage0(ioc,ii,mptfc_register_dev);
  497. }
  498. /* delete devices still missing */
  499. spin_lock_irqsave(&ioc->fc_rport_lock, flags);
  500. list_for_each_entry(ri, &ioc->fc_rports, list) {
  501. /* if newly missing, delete it */
  502. if ((ri->flags & (MPT_RPORT_INFO_FLAGS_REGISTERED |
  503. MPT_RPORT_INFO_FLAGS_MISSING))
  504. == (MPT_RPORT_INFO_FLAGS_REGISTERED |
  505. MPT_RPORT_INFO_FLAGS_MISSING)) {
  506. ri->flags &= ~(MPT_RPORT_INFO_FLAGS_REGISTERED|
  507. MPT_RPORT_INFO_FLAGS_MISSING);
  508. fc_remote_port_delete(ri->rport);
  509. /*
  510. * remote port not really deleted 'cause
  511. * binding is by WWPN and driver only
  512. * registers FCP_TARGETs
  513. */
  514. #ifdef MPT_DEBUG
  515. printk ("mptfc_rescan.%d: %llx deleted\n",
  516. ioc->sh->host_no, ri->pg0.WWPN);
  517. #endif
  518. }
  519. }
  520. spin_unlock_irqrestore(&ioc->fc_rport_lock,flags);
  521. /*
  522. * allow multiple passes as target state
  523. * might have changed during scan
  524. */
  525. spin_lock_irqsave(&ioc->fc_rescan_work_lock, flags);
  526. if (ioc->fc_rescan_work_count > 2) /* only need one more */
  527. ioc->fc_rescan_work_count = 2;
  528. work_to_do = --ioc->fc_rescan_work_count;
  529. spin_unlock_irqrestore(&ioc->fc_rescan_work_lock, flags);
  530. } while (work_to_do);
  531. }
  532. static int
  533. mptfc_probe(struct pci_dev *pdev, const struct pci_device_id *id)
  534. {
  535. struct Scsi_Host *sh;
  536. MPT_SCSI_HOST *hd;
  537. MPT_ADAPTER *ioc;
  538. unsigned long flags;
  539. int ii;
  540. int numSGE = 0;
  541. int scale;
  542. int ioc_cap;
  543. int error=0;
  544. int r;
  545. if ((r = mpt_attach(pdev,id)) != 0)
  546. return r;
  547. ioc = pci_get_drvdata(pdev);
  548. ioc->DoneCtx = mptfcDoneCtx;
  549. ioc->TaskCtx = mptfcTaskCtx;
  550. ioc->InternalCtx = mptfcInternalCtx;
  551. /* Added sanity check on readiness of the MPT adapter.
  552. */
  553. if (ioc->last_state != MPI_IOC_STATE_OPERATIONAL) {
  554. printk(MYIOC_s_WARN_FMT
  555. "Skipping because it's not operational!\n",
  556. ioc->name);
  557. error = -ENODEV;
  558. goto out_mptfc_probe;
  559. }
  560. if (!ioc->active) {
  561. printk(MYIOC_s_WARN_FMT "Skipping because it's disabled!\n",
  562. ioc->name);
  563. error = -ENODEV;
  564. goto out_mptfc_probe;
  565. }
  566. /* Sanity check - ensure at least 1 port is INITIATOR capable
  567. */
  568. ioc_cap = 0;
  569. for (ii=0; ii < ioc->facts.NumberOfPorts; ii++) {
  570. if (ioc->pfacts[ii].ProtocolFlags &
  571. MPI_PORTFACTS_PROTOCOL_INITIATOR)
  572. ioc_cap ++;
  573. }
  574. if (!ioc_cap) {
  575. printk(MYIOC_s_WARN_FMT
  576. "Skipping ioc=%p because SCSI Initiator mode is NOT enabled!\n",
  577. ioc->name, ioc);
  578. return -ENODEV;
  579. }
  580. sh = scsi_host_alloc(&mptfc_driver_template, sizeof(MPT_SCSI_HOST));
  581. if (!sh) {
  582. printk(MYIOC_s_WARN_FMT
  583. "Unable to register controller with SCSI subsystem\n",
  584. ioc->name);
  585. error = -1;
  586. goto out_mptfc_probe;
  587. }
  588. INIT_WORK(&ioc->fc_rescan_work, mptfc_rescan_devices,(void *)ioc);
  589. spin_lock_irqsave(&ioc->FreeQlock, flags);
  590. /* Attach the SCSI Host to the IOC structure
  591. */
  592. ioc->sh = sh;
  593. sh->io_port = 0;
  594. sh->n_io_port = 0;
  595. sh->irq = 0;
  596. /* set 16 byte cdb's */
  597. sh->max_cmd_len = 16;
  598. sh->max_id = MPT_MAX_FC_DEVICES<256 ? MPT_MAX_FC_DEVICES : 255;
  599. sh->max_lun = MPT_LAST_LUN + 1;
  600. sh->max_channel = 0;
  601. sh->this_id = ioc->pfacts[0].PortSCSIID;
  602. /* Required entry.
  603. */
  604. sh->unique_id = ioc->id;
  605. /* Verify that we won't exceed the maximum
  606. * number of chain buffers
  607. * We can optimize: ZZ = req_sz/sizeof(SGE)
  608. * For 32bit SGE's:
  609. * numSGE = 1 + (ZZ-1)*(maxChain -1) + ZZ
  610. * + (req_sz - 64)/sizeof(SGE)
  611. * A slightly different algorithm is required for
  612. * 64bit SGEs.
  613. */
  614. scale = ioc->req_sz/(sizeof(dma_addr_t) + sizeof(u32));
  615. if (sizeof(dma_addr_t) == sizeof(u64)) {
  616. numSGE = (scale - 1) *
  617. (ioc->facts.MaxChainDepth-1) + scale +
  618. (ioc->req_sz - 60) / (sizeof(dma_addr_t) +
  619. sizeof(u32));
  620. } else {
  621. numSGE = 1 + (scale - 1) *
  622. (ioc->facts.MaxChainDepth-1) + scale +
  623. (ioc->req_sz - 64) / (sizeof(dma_addr_t) +
  624. sizeof(u32));
  625. }
  626. if (numSGE < sh->sg_tablesize) {
  627. /* Reset this value */
  628. dprintk((MYIOC_s_INFO_FMT
  629. "Resetting sg_tablesize to %d from %d\n",
  630. ioc->name, numSGE, sh->sg_tablesize));
  631. sh->sg_tablesize = numSGE;
  632. }
  633. spin_unlock_irqrestore(&ioc->FreeQlock, flags);
  634. hd = (MPT_SCSI_HOST *) sh->hostdata;
  635. hd->ioc = ioc;
  636. /* SCSI needs scsi_cmnd lookup table!
  637. * (with size equal to req_depth*PtrSz!)
  638. */
  639. hd->ScsiLookup = kcalloc(ioc->req_depth, sizeof(void *), GFP_ATOMIC);
  640. if (!hd->ScsiLookup) {
  641. error = -ENOMEM;
  642. goto out_mptfc_probe;
  643. }
  644. dprintk((MYIOC_s_INFO_FMT "ScsiLookup @ %p\n",
  645. ioc->name, hd->ScsiLookup));
  646. /* Allocate memory for the device structures.
  647. * A non-Null pointer at an offset
  648. * indicates a device exists.
  649. * max_id = 1 + maximum id (hosts.h)
  650. */
  651. hd->Targets = kcalloc(sh->max_id, sizeof(void *), GFP_ATOMIC);
  652. if (!hd->Targets) {
  653. error = -ENOMEM;
  654. goto out_mptfc_probe;
  655. }
  656. dprintk((KERN_INFO " vdev @ %p\n", hd->Targets));
  657. /* Clear the TM flags
  658. */
  659. hd->tmPending = 0;
  660. hd->tmState = TM_STATE_NONE;
  661. hd->resetPending = 0;
  662. hd->abortSCpnt = NULL;
  663. /* Clear the pointer used to store
  664. * single-threaded commands, i.e., those
  665. * issued during a bus scan, dv and
  666. * configuration pages.
  667. */
  668. hd->cmdPtr = NULL;
  669. /* Initialize this SCSI Hosts' timers
  670. * To use, set the timer expires field
  671. * and add_timer
  672. */
  673. init_timer(&hd->timer);
  674. hd->timer.data = (unsigned long) hd;
  675. hd->timer.function = mptscsih_timer_expired;
  676. hd->mpt_pq_filter = mpt_pq_filter;
  677. ddvprintk((MYIOC_s_INFO_FMT
  678. "mpt_pq_filter %x\n",
  679. ioc->name,
  680. mpt_pq_filter));
  681. init_waitqueue_head(&hd->scandv_waitq);
  682. hd->scandv_wait_done = 0;
  683. hd->last_queue_full = 0;
  684. sh->transportt = mptfc_transport_template;
  685. error = scsi_add_host (sh, &ioc->pcidev->dev);
  686. if(error) {
  687. dprintk((KERN_ERR MYNAM
  688. "scsi_add_host failed\n"));
  689. goto out_mptfc_probe;
  690. }
  691. for (ii=0; ii < ioc->facts.NumberOfPorts; ii++) {
  692. mptfc_init_host_attr(ioc,ii);
  693. mptfc_GetFcDevPage0(ioc,ii,mptfc_register_dev);
  694. }
  695. return 0;
  696. out_mptfc_probe:
  697. mptscsih_remove(pdev);
  698. return error;
  699. }
  700. static struct pci_driver mptfc_driver = {
  701. .name = "mptfc",
  702. .id_table = mptfc_pci_table,
  703. .probe = mptfc_probe,
  704. .remove = __devexit_p(mptfc_remove),
  705. .shutdown = mptscsih_shutdown,
  706. #ifdef CONFIG_PM
  707. .suspend = mptscsih_suspend,
  708. .resume = mptscsih_resume,
  709. #endif
  710. };
  711. /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
  712. /**
  713. * mptfc_init - Register MPT adapter(s) as SCSI host(s) with
  714. * linux scsi mid-layer.
  715. *
  716. * Returns 0 for success, non-zero for failure.
  717. */
  718. static int __init
  719. mptfc_init(void)
  720. {
  721. int error;
  722. show_mptmod_ver(my_NAME, my_VERSION);
  723. /* sanity check module parameter */
  724. if (mptfc_dev_loss_tmo == 0)
  725. mptfc_dev_loss_tmo = MPTFC_DEV_LOSS_TMO;
  726. mptfc_transport_template =
  727. fc_attach_transport(&mptfc_transport_functions);
  728. if (!mptfc_transport_template)
  729. return -ENODEV;
  730. mptfcDoneCtx = mpt_register(mptscsih_io_done, MPTFC_DRIVER);
  731. mptfcTaskCtx = mpt_register(mptscsih_taskmgmt_complete, MPTFC_DRIVER);
  732. mptfcInternalCtx = mpt_register(mptscsih_scandv_complete, MPTFC_DRIVER);
  733. if (mpt_event_register(mptfcDoneCtx, mptscsih_event_process) == 0) {
  734. devtprintk((KERN_INFO MYNAM
  735. ": Registered for IOC event notifications\n"));
  736. }
  737. if (mpt_reset_register(mptfcDoneCtx, mptscsih_ioc_reset) == 0) {
  738. dprintk((KERN_INFO MYNAM
  739. ": Registered for IOC reset notifications\n"));
  740. }
  741. error = pci_register_driver(&mptfc_driver);
  742. if (error) {
  743. fc_release_transport(mptfc_transport_template);
  744. }
  745. return error;
  746. }
  747. /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
  748. /**
  749. * mptfc_remove - Removed fc infrastructure for devices
  750. * @pdev: Pointer to pci_dev structure
  751. *
  752. */
  753. static void __devexit mptfc_remove(struct pci_dev *pdev)
  754. {
  755. MPT_ADAPTER *ioc = pci_get_drvdata(pdev);
  756. struct mptfc_rport_info *p, *n;
  757. fc_remove_host(ioc->sh);
  758. list_for_each_entry_safe(p, n, &ioc->fc_rports, list) {
  759. list_del(&p->list);
  760. kfree(p);
  761. }
  762. mptscsih_remove(pdev);
  763. }
  764. /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
  765. /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
  766. /**
  767. * mptfc_exit - Unregisters MPT adapter(s)
  768. *
  769. */
  770. static void __exit
  771. mptfc_exit(void)
  772. {
  773. pci_unregister_driver(&mptfc_driver);
  774. fc_release_transport(mptfc_transport_template);
  775. mpt_reset_deregister(mptfcDoneCtx);
  776. dprintk((KERN_INFO MYNAM
  777. ": Deregistered for IOC reset notifications\n"));
  778. mpt_event_deregister(mptfcDoneCtx);
  779. dprintk((KERN_INFO MYNAM
  780. ": Deregistered for IOC event notifications\n"));
  781. mpt_deregister(mptfcInternalCtx);
  782. mpt_deregister(mptfcTaskCtx);
  783. mpt_deregister(mptfcDoneCtx);
  784. }
  785. module_init(mptfc_init);
  786. module_exit(mptfc_exit);