mptfc.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023
  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. static 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. return 0;
  298. }
  299. static void
  300. mptfc_register_dev(MPT_ADAPTER *ioc, int channel, FCDevicePage0_t *pg0)
  301. {
  302. struct fc_rport_identifiers rport_ids;
  303. struct fc_rport *rport;
  304. struct mptfc_rport_info *ri;
  305. int new_ri = 1;
  306. u64 pn, nn;
  307. VirtTarget *vtarget;
  308. u32 roles = FC_RPORT_ROLE_UNKNOWN;
  309. if (mptfc_generate_rport_ids(pg0, &rport_ids) < 0)
  310. return;
  311. roles |= FC_RPORT_ROLE_FCP_TARGET;
  312. if (pg0->Protocol & MPI_FC_DEVICE_PAGE0_PROT_FCP_INITIATOR)
  313. roles |= FC_RPORT_ROLE_FCP_INITIATOR;
  314. /* scan list looking for a match */
  315. list_for_each_entry(ri, &ioc->fc_rports, list) {
  316. pn = (u64)ri->pg0.WWPN.High << 32 | (u64)ri->pg0.WWPN.Low;
  317. if (pn == rport_ids.port_name) { /* match */
  318. list_move_tail(&ri->list, &ioc->fc_rports);
  319. new_ri = 0;
  320. break;
  321. }
  322. }
  323. if (new_ri) { /* allocate one */
  324. ri = kzalloc(sizeof(struct mptfc_rport_info), GFP_KERNEL);
  325. if (!ri)
  326. return;
  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. /* MPT_RPORT_INFO_FLAGS_REGISTERED - rport not previously deleted */
  332. if (!(ri->flags & MPT_RPORT_INFO_FLAGS_REGISTERED)) {
  333. ri->flags |= MPT_RPORT_INFO_FLAGS_REGISTERED;
  334. rport = fc_remote_port_add(ioc->sh, channel, &rport_ids);
  335. if (rport) {
  336. ri->rport = rport;
  337. if (new_ri) /* may have been reset by user */
  338. rport->dev_loss_tmo = mptfc_dev_loss_tmo;
  339. /*
  340. * if already mapped, remap here. If not mapped,
  341. * target_alloc will allocate vtarget and map,
  342. * slave_alloc will fill in vdev from vtarget.
  343. */
  344. if (ri->starget) {
  345. vtarget = ri->starget->hostdata;
  346. if (vtarget) {
  347. vtarget->target_id = pg0->CurrentTargetID;
  348. vtarget->bus_id = pg0->CurrentBus;
  349. }
  350. }
  351. *((struct mptfc_rport_info **)rport->dd_data) = ri;
  352. /* scan will be scheduled once rport becomes a target */
  353. fc_remote_port_rolechg(rport,roles);
  354. pn = (u64)ri->pg0.WWPN.High << 32 | (u64)ri->pg0.WWPN.Low;
  355. nn = (u64)ri->pg0.WWNN.High << 32 | (u64)ri->pg0.WWNN.Low;
  356. dfcprintk ((MYIOC_s_INFO_FMT
  357. "mptfc_reg_dev.%d: %x, %llx / %llx, tid %d, "
  358. "rport tid %d, tmo %d\n",
  359. ioc->name,
  360. ioc->sh->host_no,
  361. pg0->PortIdentifier,
  362. (unsigned long long)nn,
  363. (unsigned long long)pn,
  364. pg0->CurrentTargetID,
  365. ri->rport->scsi_target_id,
  366. ri->rport->dev_loss_tmo));
  367. } else {
  368. list_del(&ri->list);
  369. kfree(ri);
  370. ri = NULL;
  371. }
  372. }
  373. }
  374. /*
  375. * OS entry point to allow for host driver to free allocated memory
  376. * Called if no device present or device being unloaded
  377. */
  378. static void
  379. mptfc_target_destroy(struct scsi_target *starget)
  380. {
  381. struct fc_rport *rport;
  382. struct mptfc_rport_info *ri;
  383. rport = starget_to_rport(starget);
  384. if (rport) {
  385. ri = *((struct mptfc_rport_info **)rport->dd_data);
  386. if (ri) /* better be! */
  387. ri->starget = NULL;
  388. }
  389. if (starget->hostdata)
  390. kfree(starget->hostdata);
  391. starget->hostdata = NULL;
  392. }
  393. /*
  394. * OS entry point to allow host driver to alloc memory
  395. * for each scsi target. Called once per device the bus scan.
  396. * Return non-zero if allocation fails.
  397. */
  398. static int
  399. mptfc_target_alloc(struct scsi_target *starget)
  400. {
  401. VirtTarget *vtarget;
  402. struct fc_rport *rport;
  403. struct mptfc_rport_info *ri;
  404. int rc;
  405. vtarget = kzalloc(sizeof(VirtTarget), GFP_KERNEL);
  406. if (!vtarget)
  407. return -ENOMEM;
  408. starget->hostdata = vtarget;
  409. rc = -ENODEV;
  410. rport = starget_to_rport(starget);
  411. if (rport) {
  412. ri = *((struct mptfc_rport_info **)rport->dd_data);
  413. if (ri) { /* better be! */
  414. vtarget->target_id = ri->pg0.CurrentTargetID;
  415. vtarget->bus_id = ri->pg0.CurrentBus;
  416. ri->starget = starget;
  417. rc = 0;
  418. }
  419. }
  420. if (rc != 0) {
  421. kfree(vtarget);
  422. starget->hostdata = NULL;
  423. }
  424. return rc;
  425. }
  426. /*
  427. * OS entry point to allow host driver to alloc memory
  428. * for each scsi device. Called once per device the bus scan.
  429. * Return non-zero if allocation fails.
  430. * Init memory once per LUN.
  431. */
  432. static int
  433. mptfc_slave_alloc(struct scsi_device *sdev)
  434. {
  435. MPT_SCSI_HOST *hd;
  436. VirtTarget *vtarget;
  437. VirtDevice *vdev;
  438. struct scsi_target *starget;
  439. struct fc_rport *rport;
  440. starget = scsi_target(sdev);
  441. rport = starget_to_rport(starget);
  442. if (!rport || fc_remote_port_chkready(rport))
  443. return -ENXIO;
  444. hd = (MPT_SCSI_HOST *)sdev->host->hostdata;
  445. vdev = kzalloc(sizeof(VirtDevice), GFP_KERNEL);
  446. if (!vdev) {
  447. printk(MYIOC_s_ERR_FMT "slave_alloc kmalloc(%zd) FAILED!\n",
  448. hd->ioc->name, sizeof(VirtDevice));
  449. return -ENOMEM;
  450. }
  451. sdev->hostdata = vdev;
  452. vtarget = starget->hostdata;
  453. if (vtarget->num_luns == 0) {
  454. vtarget->ioc_id = hd->ioc->id;
  455. vtarget->tflags = MPT_TARGET_FLAGS_Q_YES |
  456. MPT_TARGET_FLAGS_VALID_INQUIRY;
  457. hd->Targets[sdev->id] = vtarget;
  458. }
  459. vdev->vtarget = vtarget;
  460. vdev->lun = sdev->lun;
  461. vtarget->num_luns++;
  462. #ifdef DMPT_DEBUG_FC
  463. {
  464. u64 nn, pn;
  465. struct mptfc_rport_info *ri;
  466. ri = *((struct mptfc_rport_info **)rport->dd_data);
  467. pn = (u64)ri->pg0.WWPN.High << 32 | (u64)ri->pg0.WWPN.Low;
  468. nn = (u64)ri->pg0.WWNN.High << 32 | (u64)ri->pg0.WWNN.Low;
  469. dfcprintk ((MYIOC_s_INFO_FMT
  470. "mptfc_slv_alloc.%d: num_luns %d, sdev.id %d, "
  471. "CurrentTargetID %d, %x %llx %llx\n",
  472. hd->ioc->name,
  473. sdev->host->host_no,
  474. vtarget->num_luns,
  475. sdev->id, ri->pg0.CurrentTargetID,
  476. ri->pg0.PortIdentifier,
  477. (unsigned long long)pn,
  478. (unsigned long long)nn));
  479. }
  480. #endif
  481. return 0;
  482. }
  483. static int
  484. mptfc_qcmd(struct scsi_cmnd *SCpnt, void (*done)(struct scsi_cmnd *))
  485. {
  486. struct mptfc_rport_info *ri;
  487. struct fc_rport *rport = starget_to_rport(scsi_target(SCpnt->device));
  488. int err;
  489. err = fc_remote_port_chkready(rport);
  490. if (unlikely(err)) {
  491. SCpnt->result = err;
  492. done(SCpnt);
  493. return 0;
  494. }
  495. /* dd_data is null until finished adding target */
  496. ri = *((struct mptfc_rport_info **)rport->dd_data);
  497. if (unlikely(!ri)) {
  498. dfcprintk ((MYIOC_s_INFO_FMT
  499. "mptfc_qcmd.%d: %d:%d, dd_data is null.\n",
  500. ((MPT_SCSI_HOST *) SCpnt->device->host->hostdata)->ioc->name,
  501. ((MPT_SCSI_HOST *) SCpnt->device->host->hostdata)->ioc->sh->host_no,
  502. SCpnt->device->id,SCpnt->device->lun));
  503. SCpnt->result = DID_IMM_RETRY << 16;
  504. done(SCpnt);
  505. return 0;
  506. }
  507. err = mptscsih_qcmd(SCpnt,done);
  508. #ifdef DMPT_DEBUG_FC
  509. if (unlikely(err)) {
  510. dfcprintk ((MYIOC_s_INFO_FMT
  511. "mptfc_qcmd.%d: %d:%d, mptscsih_qcmd returns non-zero.\n",
  512. ((MPT_SCSI_HOST *) SCpnt->device->host->hostdata)->ioc->name,
  513. ((MPT_SCSI_HOST *) SCpnt->device->host->hostdata)->ioc->sh->host_no,
  514. SCpnt->device->id,SCpnt->device->lun));
  515. }
  516. #endif
  517. return err;
  518. }
  519. static void
  520. mptfc_init_host_attr(MPT_ADAPTER *ioc,int portnum)
  521. {
  522. unsigned class = 0, cos = 0;
  523. /* don't know what to do as only one scsi (fc) host was allocated */
  524. if (portnum != 0)
  525. return;
  526. class = ioc->fc_port_page0[portnum].SupportedServiceClass;
  527. if (class & MPI_FCPORTPAGE0_SUPPORT_CLASS_1)
  528. cos |= FC_COS_CLASS1;
  529. if (class & MPI_FCPORTPAGE0_SUPPORT_CLASS_2)
  530. cos |= FC_COS_CLASS2;
  531. if (class & MPI_FCPORTPAGE0_SUPPORT_CLASS_3)
  532. cos |= FC_COS_CLASS3;
  533. fc_host_node_name(ioc->sh) =
  534. (u64)ioc->fc_port_page0[portnum].WWNN.High << 32
  535. | (u64)ioc->fc_port_page0[portnum].WWNN.Low;
  536. fc_host_port_name(ioc->sh) =
  537. (u64)ioc->fc_port_page0[portnum].WWPN.High << 32
  538. | (u64)ioc->fc_port_page0[portnum].WWPN.Low;
  539. fc_host_port_id(ioc->sh) = ioc->fc_port_page0[portnum].PortIdentifier;
  540. fc_host_supported_classes(ioc->sh) = cos;
  541. fc_host_tgtid_bind_type(ioc->sh) = FC_TGTID_BIND_BY_WWPN;
  542. }
  543. static void
  544. mptfc_rescan_devices(void *arg)
  545. {
  546. MPT_ADAPTER *ioc = (MPT_ADAPTER *)arg;
  547. int ii;
  548. int work_to_do;
  549. u64 pn;
  550. unsigned long flags;
  551. struct mptfc_rport_info *ri;
  552. do {
  553. /* start by tagging all ports as missing */
  554. list_for_each_entry(ri, &ioc->fc_rports, list) {
  555. if (ri->flags & MPT_RPORT_INFO_FLAGS_REGISTERED) {
  556. ri->flags |= MPT_RPORT_INFO_FLAGS_MISSING;
  557. }
  558. }
  559. /*
  560. * now rescan devices known to adapter,
  561. * will reregister existing rports
  562. */
  563. for (ii=0; ii < ioc->facts.NumberOfPorts; ii++) {
  564. (void) mptbase_GetFcPortPage0(ioc, ii);
  565. mptfc_init_host_attr(ioc,ii); /* refresh */
  566. mptfc_GetFcDevPage0(ioc,ii,mptfc_register_dev);
  567. }
  568. /* delete devices still missing */
  569. list_for_each_entry(ri, &ioc->fc_rports, list) {
  570. /* if newly missing, delete it */
  571. if (ri->flags & MPT_RPORT_INFO_FLAGS_MISSING) {
  572. ri->flags &= ~(MPT_RPORT_INFO_FLAGS_REGISTERED|
  573. MPT_RPORT_INFO_FLAGS_MISSING);
  574. fc_remote_port_delete(ri->rport); /* won't sleep */
  575. ri->rport = NULL;
  576. pn = (u64)ri->pg0.WWPN.High << 32 |
  577. (u64)ri->pg0.WWPN.Low;
  578. dfcprintk ((MYIOC_s_INFO_FMT
  579. "mptfc_rescan.%d: %llx deleted\n",
  580. ioc->name,
  581. ioc->sh->host_no,
  582. (unsigned long long)pn));
  583. }
  584. }
  585. /*
  586. * allow multiple passes as target state
  587. * might have changed during scan
  588. */
  589. spin_lock_irqsave(&ioc->fc_rescan_work_lock, flags);
  590. if (ioc->fc_rescan_work_count > 2) /* only need one more */
  591. ioc->fc_rescan_work_count = 2;
  592. work_to_do = --ioc->fc_rescan_work_count;
  593. spin_unlock_irqrestore(&ioc->fc_rescan_work_lock, flags);
  594. } while (work_to_do);
  595. }
  596. static int
  597. mptfc_probe(struct pci_dev *pdev, const struct pci_device_id *id)
  598. {
  599. struct Scsi_Host *sh;
  600. MPT_SCSI_HOST *hd;
  601. MPT_ADAPTER *ioc;
  602. unsigned long flags;
  603. int ii;
  604. int numSGE = 0;
  605. int scale;
  606. int ioc_cap;
  607. int error=0;
  608. int r;
  609. if ((r = mpt_attach(pdev,id)) != 0)
  610. return r;
  611. ioc = pci_get_drvdata(pdev);
  612. ioc->DoneCtx = mptfcDoneCtx;
  613. ioc->TaskCtx = mptfcTaskCtx;
  614. ioc->InternalCtx = mptfcInternalCtx;
  615. /* Added sanity check on readiness of the MPT adapter.
  616. */
  617. if (ioc->last_state != MPI_IOC_STATE_OPERATIONAL) {
  618. printk(MYIOC_s_WARN_FMT
  619. "Skipping because it's not operational!\n",
  620. ioc->name);
  621. error = -ENODEV;
  622. goto out_mptfc_probe;
  623. }
  624. if (!ioc->active) {
  625. printk(MYIOC_s_WARN_FMT "Skipping because it's disabled!\n",
  626. ioc->name);
  627. error = -ENODEV;
  628. goto out_mptfc_probe;
  629. }
  630. /* Sanity check - ensure at least 1 port is INITIATOR capable
  631. */
  632. ioc_cap = 0;
  633. for (ii=0; ii < ioc->facts.NumberOfPorts; ii++) {
  634. if (ioc->pfacts[ii].ProtocolFlags &
  635. MPI_PORTFACTS_PROTOCOL_INITIATOR)
  636. ioc_cap ++;
  637. }
  638. if (!ioc_cap) {
  639. printk(MYIOC_s_WARN_FMT
  640. "Skipping ioc=%p because SCSI Initiator mode is NOT enabled!\n",
  641. ioc->name, ioc);
  642. return -ENODEV;
  643. }
  644. sh = scsi_host_alloc(&mptfc_driver_template, sizeof(MPT_SCSI_HOST));
  645. if (!sh) {
  646. printk(MYIOC_s_WARN_FMT
  647. "Unable to register controller with SCSI subsystem\n",
  648. ioc->name);
  649. error = -1;
  650. goto out_mptfc_probe;
  651. }
  652. INIT_WORK(&ioc->fc_rescan_work, mptfc_rescan_devices,(void *)ioc);
  653. spin_lock_irqsave(&ioc->FreeQlock, flags);
  654. /* Attach the SCSI Host to the IOC structure
  655. */
  656. ioc->sh = sh;
  657. sh->io_port = 0;
  658. sh->n_io_port = 0;
  659. sh->irq = 0;
  660. /* set 16 byte cdb's */
  661. sh->max_cmd_len = 16;
  662. sh->max_id = MPT_MAX_FC_DEVICES<256 ? MPT_MAX_FC_DEVICES : 255;
  663. sh->max_lun = MPT_LAST_LUN + 1;
  664. sh->max_channel = 0;
  665. sh->this_id = ioc->pfacts[0].PortSCSIID;
  666. /* Required entry.
  667. */
  668. sh->unique_id = ioc->id;
  669. /* Verify that we won't exceed the maximum
  670. * number of chain buffers
  671. * We can optimize: ZZ = req_sz/sizeof(SGE)
  672. * For 32bit SGE's:
  673. * numSGE = 1 + (ZZ-1)*(maxChain -1) + ZZ
  674. * + (req_sz - 64)/sizeof(SGE)
  675. * A slightly different algorithm is required for
  676. * 64bit SGEs.
  677. */
  678. scale = ioc->req_sz/(sizeof(dma_addr_t) + sizeof(u32));
  679. if (sizeof(dma_addr_t) == sizeof(u64)) {
  680. numSGE = (scale - 1) *
  681. (ioc->facts.MaxChainDepth-1) + scale +
  682. (ioc->req_sz - 60) / (sizeof(dma_addr_t) +
  683. sizeof(u32));
  684. } else {
  685. numSGE = 1 + (scale - 1) *
  686. (ioc->facts.MaxChainDepth-1) + scale +
  687. (ioc->req_sz - 64) / (sizeof(dma_addr_t) +
  688. sizeof(u32));
  689. }
  690. if (numSGE < sh->sg_tablesize) {
  691. /* Reset this value */
  692. dprintk((MYIOC_s_INFO_FMT
  693. "Resetting sg_tablesize to %d from %d\n",
  694. ioc->name, numSGE, sh->sg_tablesize));
  695. sh->sg_tablesize = numSGE;
  696. }
  697. spin_unlock_irqrestore(&ioc->FreeQlock, flags);
  698. hd = (MPT_SCSI_HOST *) sh->hostdata;
  699. hd->ioc = ioc;
  700. /* SCSI needs scsi_cmnd lookup table!
  701. * (with size equal to req_depth*PtrSz!)
  702. */
  703. hd->ScsiLookup = kcalloc(ioc->req_depth, sizeof(void *), GFP_ATOMIC);
  704. if (!hd->ScsiLookup) {
  705. error = -ENOMEM;
  706. goto out_mptfc_probe;
  707. }
  708. dprintk((MYIOC_s_INFO_FMT "ScsiLookup @ %p\n",
  709. ioc->name, hd->ScsiLookup));
  710. /* Allocate memory for the device structures.
  711. * A non-Null pointer at an offset
  712. * indicates a device exists.
  713. * max_id = 1 + maximum id (hosts.h)
  714. */
  715. hd->Targets = kcalloc(sh->max_id, sizeof(void *), GFP_ATOMIC);
  716. if (!hd->Targets) {
  717. error = -ENOMEM;
  718. goto out_mptfc_probe;
  719. }
  720. dprintk((KERN_INFO " vdev @ %p\n", hd->Targets));
  721. /* Clear the TM flags
  722. */
  723. hd->tmPending = 0;
  724. hd->tmState = TM_STATE_NONE;
  725. hd->resetPending = 0;
  726. hd->abortSCpnt = NULL;
  727. /* Clear the pointer used to store
  728. * single-threaded commands, i.e., those
  729. * issued during a bus scan, dv and
  730. * configuration pages.
  731. */
  732. hd->cmdPtr = NULL;
  733. /* Initialize this SCSI Hosts' timers
  734. * To use, set the timer expires field
  735. * and add_timer
  736. */
  737. init_timer(&hd->timer);
  738. hd->timer.data = (unsigned long) hd;
  739. hd->timer.function = mptscsih_timer_expired;
  740. hd->mpt_pq_filter = mpt_pq_filter;
  741. ddvprintk((MYIOC_s_INFO_FMT
  742. "mpt_pq_filter %x\n",
  743. ioc->name,
  744. mpt_pq_filter));
  745. init_waitqueue_head(&hd->scandv_waitq);
  746. hd->scandv_wait_done = 0;
  747. hd->last_queue_full = 0;
  748. sh->transportt = mptfc_transport_template;
  749. error = scsi_add_host (sh, &ioc->pcidev->dev);
  750. if(error) {
  751. dprintk((KERN_ERR MYNAM
  752. "scsi_add_host failed\n"));
  753. goto out_mptfc_probe;
  754. }
  755. /* initialize workqueue */
  756. snprintf(ioc->fc_rescan_work_q_name, KOBJ_NAME_LEN, "mptfc_wq_%d",
  757. sh->host_no);
  758. ioc->fc_rescan_work_q =
  759. create_singlethread_workqueue(ioc->fc_rescan_work_q_name);
  760. if (!ioc->fc_rescan_work_q)
  761. goto out_mptfc_probe;
  762. /*
  763. * scan for rports -
  764. * by doing it via the workqueue, some locking is eliminated
  765. */
  766. ioc->fc_rescan_work_count = 1;
  767. queue_work(ioc->fc_rescan_work_q, &ioc->fc_rescan_work);
  768. flush_workqueue(ioc->fc_rescan_work_q);
  769. return 0;
  770. out_mptfc_probe:
  771. mptscsih_remove(pdev);
  772. return error;
  773. }
  774. static struct pci_driver mptfc_driver = {
  775. .name = "mptfc",
  776. .id_table = mptfc_pci_table,
  777. .probe = mptfc_probe,
  778. .remove = __devexit_p(mptfc_remove),
  779. .shutdown = mptscsih_shutdown,
  780. #ifdef CONFIG_PM
  781. .suspend = mptscsih_suspend,
  782. .resume = mptscsih_resume,
  783. #endif
  784. };
  785. /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
  786. /**
  787. * mptfc_init - Register MPT adapter(s) as SCSI host(s) with
  788. * linux scsi mid-layer.
  789. *
  790. * Returns 0 for success, non-zero for failure.
  791. */
  792. static int __init
  793. mptfc_init(void)
  794. {
  795. int error;
  796. show_mptmod_ver(my_NAME, my_VERSION);
  797. /* sanity check module parameter */
  798. if (mptfc_dev_loss_tmo == 0)
  799. mptfc_dev_loss_tmo = MPTFC_DEV_LOSS_TMO;
  800. mptfc_transport_template =
  801. fc_attach_transport(&mptfc_transport_functions);
  802. if (!mptfc_transport_template)
  803. return -ENODEV;
  804. mptfcDoneCtx = mpt_register(mptscsih_io_done, MPTFC_DRIVER);
  805. mptfcTaskCtx = mpt_register(mptscsih_taskmgmt_complete, MPTFC_DRIVER);
  806. mptfcInternalCtx = mpt_register(mptscsih_scandv_complete, MPTFC_DRIVER);
  807. if (mpt_event_register(mptfcDoneCtx, mptscsih_event_process) == 0) {
  808. devtverboseprintk((KERN_INFO MYNAM
  809. ": Registered for IOC event notifications\n"));
  810. }
  811. if (mpt_reset_register(mptfcDoneCtx, mptscsih_ioc_reset) == 0) {
  812. dprintk((KERN_INFO MYNAM
  813. ": Registered for IOC reset notifications\n"));
  814. }
  815. error = pci_register_driver(&mptfc_driver);
  816. if (error)
  817. fc_release_transport(mptfc_transport_template);
  818. return error;
  819. }
  820. /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
  821. /**
  822. * mptfc_remove - Removed fc infrastructure for devices
  823. * @pdev: Pointer to pci_dev structure
  824. *
  825. */
  826. static void __devexit
  827. mptfc_remove(struct pci_dev *pdev)
  828. {
  829. MPT_ADAPTER *ioc = pci_get_drvdata(pdev);
  830. struct mptfc_rport_info *p, *n;
  831. struct workqueue_struct *work_q;
  832. unsigned long flags;
  833. /* destroy workqueue */
  834. if ((work_q=ioc->fc_rescan_work_q)) {
  835. spin_lock_irqsave(&ioc->fc_rescan_work_lock, flags);
  836. ioc->fc_rescan_work_q = NULL;
  837. spin_unlock_irqrestore(&ioc->fc_rescan_work_lock, flags);
  838. destroy_workqueue(work_q);
  839. }
  840. fc_remove_host(ioc->sh);
  841. list_for_each_entry_safe(p, n, &ioc->fc_rports, list) {
  842. list_del(&p->list);
  843. kfree(p);
  844. }
  845. mptscsih_remove(pdev);
  846. }
  847. /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
  848. /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
  849. /**
  850. * mptfc_exit - Unregisters MPT adapter(s)
  851. *
  852. */
  853. static void __exit
  854. mptfc_exit(void)
  855. {
  856. pci_unregister_driver(&mptfc_driver);
  857. fc_release_transport(mptfc_transport_template);
  858. mpt_reset_deregister(mptfcDoneCtx);
  859. dprintk((KERN_INFO MYNAM
  860. ": Deregistered for IOC reset notifications\n"));
  861. mpt_event_deregister(mptfcDoneCtx);
  862. dprintk((KERN_INFO MYNAM
  863. ": Deregistered for IOC event notifications\n"));
  864. mpt_deregister(mptfcInternalCtx);
  865. mpt_deregister(mptfcTaskCtx);
  866. mpt_deregister(mptfcDoneCtx);
  867. }
  868. module_init(mptfc_init);
  869. module_exit(mptfc_exit);