mptfc.c 27 KB

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