mptfc.c 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389
  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, MPI_MANUFACTPAGE_DEVICEID_FC909,
  121. PCI_ANY_ID, PCI_ANY_ID },
  122. { PCI_VENDOR_ID_LSI_LOGIC, MPI_MANUFACTPAGE_DEVICEID_FC919,
  123. PCI_ANY_ID, PCI_ANY_ID },
  124. { PCI_VENDOR_ID_LSI_LOGIC, MPI_MANUFACTPAGE_DEVICEID_FC929,
  125. PCI_ANY_ID, PCI_ANY_ID },
  126. { PCI_VENDOR_ID_LSI_LOGIC, MPI_MANUFACTPAGE_DEVICEID_FC919X,
  127. PCI_ANY_ID, PCI_ANY_ID },
  128. { PCI_VENDOR_ID_LSI_LOGIC, MPI_MANUFACTPAGE_DEVICEID_FC929X,
  129. PCI_ANY_ID, PCI_ANY_ID },
  130. { PCI_VENDOR_ID_LSI_LOGIC, MPI_MANUFACTPAGE_DEVICEID_FC939X,
  131. PCI_ANY_ID, PCI_ANY_ID },
  132. { PCI_VENDOR_ID_LSI_LOGIC, MPI_MANUFACTPAGE_DEVICEID_FC949X,
  133. PCI_ANY_ID, PCI_ANY_ID },
  134. { PCI_VENDOR_ID_LSI_LOGIC, MPI_MANUFACTPAGE_DEVICEID_FC949E,
  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. static void
  154. mptfc_set_rport_loss_tmo(struct fc_rport *rport, uint32_t timeout)
  155. {
  156. if (timeout > 0)
  157. rport->dev_loss_tmo = timeout;
  158. else
  159. rport->dev_loss_tmo = mptfc_dev_loss_tmo;
  160. }
  161. static int
  162. mptfc_FcDevPage0_cmp_func(const void *a, const void *b)
  163. {
  164. FCDevicePage0_t **aa = (FCDevicePage0_t **)a;
  165. FCDevicePage0_t **bb = (FCDevicePage0_t **)b;
  166. if ((*aa)->CurrentBus == (*bb)->CurrentBus) {
  167. if ((*aa)->CurrentTargetID == (*bb)->CurrentTargetID)
  168. return 0;
  169. if ((*aa)->CurrentTargetID < (*bb)->CurrentTargetID)
  170. return -1;
  171. return 1;
  172. }
  173. if ((*aa)->CurrentBus < (*bb)->CurrentBus)
  174. return -1;
  175. return 1;
  176. }
  177. static int
  178. mptfc_GetFcDevPage0(MPT_ADAPTER *ioc, int ioc_port,
  179. void(*func)(MPT_ADAPTER *ioc,int channel, FCDevicePage0_t *arg))
  180. {
  181. ConfigPageHeader_t hdr;
  182. CONFIGPARMS cfg;
  183. FCDevicePage0_t *ppage0_alloc, *fc;
  184. dma_addr_t page0_dma;
  185. int data_sz;
  186. int ii;
  187. FCDevicePage0_t *p0_array=NULL, *p_p0;
  188. FCDevicePage0_t **pp0_array=NULL, **p_pp0;
  189. int rc = -ENOMEM;
  190. U32 port_id = 0xffffff;
  191. int num_targ = 0;
  192. int max_bus = ioc->facts.MaxBuses;
  193. int max_targ = ioc->facts.MaxDevices;
  194. if (max_bus == 0 || max_targ == 0)
  195. goto out;
  196. data_sz = sizeof(FCDevicePage0_t) * max_bus * max_targ;
  197. p_p0 = p0_array = kzalloc(data_sz, GFP_KERNEL);
  198. if (!p0_array)
  199. goto out;
  200. data_sz = sizeof(FCDevicePage0_t *) * max_bus * max_targ;
  201. p_pp0 = pp0_array = kzalloc(data_sz, GFP_KERNEL);
  202. if (!pp0_array)
  203. goto out;
  204. do {
  205. /* Get FC Device Page 0 header */
  206. hdr.PageVersion = 0;
  207. hdr.PageLength = 0;
  208. hdr.PageNumber = 0;
  209. hdr.PageType = MPI_CONFIG_PAGETYPE_FC_DEVICE;
  210. cfg.cfghdr.hdr = &hdr;
  211. cfg.physAddr = -1;
  212. cfg.action = MPI_CONFIG_ACTION_PAGE_HEADER;
  213. cfg.dir = 0;
  214. cfg.pageAddr = port_id;
  215. cfg.timeout = 0;
  216. if ((rc = mpt_config(ioc, &cfg)) != 0)
  217. break;
  218. if (hdr.PageLength <= 0)
  219. break;
  220. data_sz = hdr.PageLength * 4;
  221. ppage0_alloc = pci_alloc_consistent(ioc->pcidev, data_sz,
  222. &page0_dma);
  223. rc = -ENOMEM;
  224. if (!ppage0_alloc)
  225. break;
  226. cfg.physAddr = page0_dma;
  227. cfg.action = MPI_CONFIG_ACTION_PAGE_READ_CURRENT;
  228. if ((rc = mpt_config(ioc, &cfg)) == 0) {
  229. ppage0_alloc->PortIdentifier =
  230. le32_to_cpu(ppage0_alloc->PortIdentifier);
  231. ppage0_alloc->WWNN.Low =
  232. le32_to_cpu(ppage0_alloc->WWNN.Low);
  233. ppage0_alloc->WWNN.High =
  234. le32_to_cpu(ppage0_alloc->WWNN.High);
  235. ppage0_alloc->WWPN.Low =
  236. le32_to_cpu(ppage0_alloc->WWPN.Low);
  237. ppage0_alloc->WWPN.High =
  238. le32_to_cpu(ppage0_alloc->WWPN.High);
  239. ppage0_alloc->BBCredit =
  240. le16_to_cpu(ppage0_alloc->BBCredit);
  241. ppage0_alloc->MaxRxFrameSize =
  242. le16_to_cpu(ppage0_alloc->MaxRxFrameSize);
  243. port_id = ppage0_alloc->PortIdentifier;
  244. num_targ++;
  245. *p_p0 = *ppage0_alloc; /* save data */
  246. *p_pp0++ = p_p0++; /* save addr */
  247. }
  248. pci_free_consistent(ioc->pcidev, data_sz,
  249. (u8 *) ppage0_alloc, page0_dma);
  250. if (rc != 0)
  251. break;
  252. } while (port_id <= 0xff0000);
  253. if (num_targ) {
  254. /* sort array */
  255. if (num_targ > 1)
  256. sort (pp0_array, num_targ, sizeof(FCDevicePage0_t *),
  257. mptfc_FcDevPage0_cmp_func, NULL);
  258. /* call caller's func for each targ */
  259. for (ii = 0; ii < num_targ; ii++) {
  260. fc = *(pp0_array+ii);
  261. func(ioc, ioc_port, fc);
  262. }
  263. }
  264. out:
  265. if (pp0_array)
  266. kfree(pp0_array);
  267. if (p0_array)
  268. kfree(p0_array);
  269. return rc;
  270. }
  271. static int
  272. mptfc_generate_rport_ids(FCDevicePage0_t *pg0, struct fc_rport_identifiers *rid)
  273. {
  274. /* not currently usable */
  275. if (pg0->Flags & (MPI_FC_DEVICE_PAGE0_FLAGS_PLOGI_INVALID |
  276. MPI_FC_DEVICE_PAGE0_FLAGS_PRLI_INVALID))
  277. return -1;
  278. if (!(pg0->Flags & MPI_FC_DEVICE_PAGE0_FLAGS_TARGETID_BUS_VALID))
  279. return -1;
  280. if (!(pg0->Protocol & MPI_FC_DEVICE_PAGE0_PROT_FCP_TARGET))
  281. return -1;
  282. /*
  283. * board data structure already normalized to platform endianness
  284. * shifted to avoid unaligned access on 64 bit architecture
  285. */
  286. rid->node_name = ((u64)pg0->WWNN.High) << 32 | (u64)pg0->WWNN.Low;
  287. rid->port_name = ((u64)pg0->WWPN.High) << 32 | (u64)pg0->WWPN.Low;
  288. rid->port_id = pg0->PortIdentifier;
  289. rid->roles = FC_RPORT_ROLE_UNKNOWN;
  290. return 0;
  291. }
  292. static void
  293. mptfc_register_dev(MPT_ADAPTER *ioc, int channel, FCDevicePage0_t *pg0)
  294. {
  295. struct fc_rport_identifiers rport_ids;
  296. struct fc_rport *rport;
  297. struct mptfc_rport_info *ri;
  298. int new_ri = 1;
  299. u64 pn, nn;
  300. VirtTarget *vtarget;
  301. u32 roles = FC_RPORT_ROLE_UNKNOWN;
  302. if (mptfc_generate_rport_ids(pg0, &rport_ids) < 0)
  303. return;
  304. roles |= FC_RPORT_ROLE_FCP_TARGET;
  305. if (pg0->Protocol & MPI_FC_DEVICE_PAGE0_PROT_FCP_INITIATOR)
  306. roles |= FC_RPORT_ROLE_FCP_INITIATOR;
  307. /* scan list looking for a match */
  308. list_for_each_entry(ri, &ioc->fc_rports, list) {
  309. pn = (u64)ri->pg0.WWPN.High << 32 | (u64)ri->pg0.WWPN.Low;
  310. if (pn == rport_ids.port_name) { /* match */
  311. list_move_tail(&ri->list, &ioc->fc_rports);
  312. new_ri = 0;
  313. break;
  314. }
  315. }
  316. if (new_ri) { /* allocate one */
  317. ri = kzalloc(sizeof(struct mptfc_rport_info), GFP_KERNEL);
  318. if (!ri)
  319. return;
  320. list_add_tail(&ri->list, &ioc->fc_rports);
  321. }
  322. ri->pg0 = *pg0; /* add/update pg0 data */
  323. ri->flags &= ~MPT_RPORT_INFO_FLAGS_MISSING;
  324. /* MPT_RPORT_INFO_FLAGS_REGISTERED - rport not previously deleted */
  325. if (!(ri->flags & MPT_RPORT_INFO_FLAGS_REGISTERED)) {
  326. ri->flags |= MPT_RPORT_INFO_FLAGS_REGISTERED;
  327. rport = fc_remote_port_add(ioc->sh, channel, &rport_ids);
  328. if (rport) {
  329. ri->rport = rport;
  330. if (new_ri) /* may have been reset by user */
  331. rport->dev_loss_tmo = mptfc_dev_loss_tmo;
  332. /*
  333. * if already mapped, remap here. If not mapped,
  334. * target_alloc will allocate vtarget and map,
  335. * slave_alloc will fill in vdev from vtarget.
  336. */
  337. if (ri->starget) {
  338. vtarget = ri->starget->hostdata;
  339. if (vtarget) {
  340. vtarget->target_id = pg0->CurrentTargetID;
  341. vtarget->bus_id = pg0->CurrentBus;
  342. }
  343. }
  344. *((struct mptfc_rport_info **)rport->dd_data) = ri;
  345. /* scan will be scheduled once rport becomes a target */
  346. fc_remote_port_rolechg(rport,roles);
  347. pn = (u64)ri->pg0.WWPN.High << 32 | (u64)ri->pg0.WWPN.Low;
  348. nn = (u64)ri->pg0.WWNN.High << 32 | (u64)ri->pg0.WWNN.Low;
  349. dfcprintk ((MYIOC_s_INFO_FMT
  350. "mptfc_reg_dev.%d: %x, %llx / %llx, tid %d, "
  351. "rport tid %d, tmo %d\n",
  352. ioc->name,
  353. ioc->sh->host_no,
  354. pg0->PortIdentifier,
  355. (unsigned long long)nn,
  356. (unsigned long long)pn,
  357. pg0->CurrentTargetID,
  358. ri->rport->scsi_target_id,
  359. ri->rport->dev_loss_tmo));
  360. } else {
  361. list_del(&ri->list);
  362. kfree(ri);
  363. ri = NULL;
  364. }
  365. }
  366. }
  367. /*
  368. * OS entry point to allow for host driver to free allocated memory
  369. * Called if no device present or device being unloaded
  370. */
  371. static void
  372. mptfc_target_destroy(struct scsi_target *starget)
  373. {
  374. struct fc_rport *rport;
  375. struct mptfc_rport_info *ri;
  376. rport = starget_to_rport(starget);
  377. if (rport) {
  378. ri = *((struct mptfc_rport_info **)rport->dd_data);
  379. if (ri) /* better be! */
  380. ri->starget = NULL;
  381. }
  382. if (starget->hostdata)
  383. kfree(starget->hostdata);
  384. starget->hostdata = NULL;
  385. }
  386. /*
  387. * OS entry point to allow host driver to alloc memory
  388. * for each scsi target. Called once per device the bus scan.
  389. * Return non-zero if allocation fails.
  390. */
  391. static int
  392. mptfc_target_alloc(struct scsi_target *starget)
  393. {
  394. VirtTarget *vtarget;
  395. struct fc_rport *rport;
  396. struct mptfc_rport_info *ri;
  397. int rc;
  398. vtarget = kzalloc(sizeof(VirtTarget), GFP_KERNEL);
  399. if (!vtarget)
  400. return -ENOMEM;
  401. starget->hostdata = vtarget;
  402. rc = -ENODEV;
  403. rport = starget_to_rport(starget);
  404. if (rport) {
  405. ri = *((struct mptfc_rport_info **)rport->dd_data);
  406. if (ri) { /* better be! */
  407. vtarget->target_id = ri->pg0.CurrentTargetID;
  408. vtarget->bus_id = ri->pg0.CurrentBus;
  409. ri->starget = starget;
  410. rc = 0;
  411. }
  412. }
  413. if (rc != 0) {
  414. kfree(vtarget);
  415. starget->hostdata = NULL;
  416. }
  417. return rc;
  418. }
  419. /*
  420. * OS entry point to allow host driver to alloc memory
  421. * for each scsi device. Called once per device the bus scan.
  422. * Return non-zero if allocation fails.
  423. * Init memory once per LUN.
  424. */
  425. static int
  426. mptfc_slave_alloc(struct scsi_device *sdev)
  427. {
  428. MPT_SCSI_HOST *hd;
  429. VirtTarget *vtarget;
  430. VirtDevice *vdev;
  431. struct scsi_target *starget;
  432. struct fc_rport *rport;
  433. starget = scsi_target(sdev);
  434. rport = starget_to_rport(starget);
  435. if (!rport || fc_remote_port_chkready(rport))
  436. return -ENXIO;
  437. hd = (MPT_SCSI_HOST *)sdev->host->hostdata;
  438. vdev = kzalloc(sizeof(VirtDevice), GFP_KERNEL);
  439. if (!vdev) {
  440. printk(MYIOC_s_ERR_FMT "slave_alloc kmalloc(%zd) FAILED!\n",
  441. hd->ioc->name, sizeof(VirtDevice));
  442. return -ENOMEM;
  443. }
  444. sdev->hostdata = vdev;
  445. vtarget = starget->hostdata;
  446. if (vtarget->num_luns == 0) {
  447. vtarget->ioc_id = hd->ioc->id;
  448. vtarget->tflags = MPT_TARGET_FLAGS_Q_YES |
  449. MPT_TARGET_FLAGS_VALID_INQUIRY;
  450. hd->Targets[sdev->id] = vtarget;
  451. }
  452. vdev->vtarget = vtarget;
  453. vdev->lun = sdev->lun;
  454. vtarget->num_luns++;
  455. #ifdef DMPT_DEBUG_FC
  456. {
  457. u64 nn, pn;
  458. struct mptfc_rport_info *ri;
  459. ri = *((struct mptfc_rport_info **)rport->dd_data);
  460. pn = (u64)ri->pg0.WWPN.High << 32 | (u64)ri->pg0.WWPN.Low;
  461. nn = (u64)ri->pg0.WWNN.High << 32 | (u64)ri->pg0.WWNN.Low;
  462. dfcprintk ((MYIOC_s_INFO_FMT
  463. "mptfc_slv_alloc.%d: num_luns %d, sdev.id %d, "
  464. "CurrentTargetID %d, %x %llx %llx\n",
  465. hd->ioc->name,
  466. sdev->host->host_no,
  467. vtarget->num_luns,
  468. sdev->id, ri->pg0.CurrentTargetID,
  469. ri->pg0.PortIdentifier,
  470. (unsigned long long)pn,
  471. (unsigned long long)nn));
  472. }
  473. #endif
  474. return 0;
  475. }
  476. static int
  477. mptfc_qcmd(struct scsi_cmnd *SCpnt, void (*done)(struct scsi_cmnd *))
  478. {
  479. struct mptfc_rport_info *ri;
  480. struct fc_rport *rport = starget_to_rport(scsi_target(SCpnt->device));
  481. int err;
  482. err = fc_remote_port_chkready(rport);
  483. if (unlikely(err)) {
  484. SCpnt->result = err;
  485. done(SCpnt);
  486. return 0;
  487. }
  488. /* dd_data is null until finished adding target */
  489. ri = *((struct mptfc_rport_info **)rport->dd_data);
  490. if (unlikely(!ri)) {
  491. dfcprintk ((MYIOC_s_INFO_FMT
  492. "mptfc_qcmd.%d: %d:%d, dd_data is null.\n",
  493. ((MPT_SCSI_HOST *) SCpnt->device->host->hostdata)->ioc->name,
  494. ((MPT_SCSI_HOST *) SCpnt->device->host->hostdata)->ioc->sh->host_no,
  495. SCpnt->device->id,SCpnt->device->lun));
  496. SCpnt->result = DID_IMM_RETRY << 16;
  497. done(SCpnt);
  498. return 0;
  499. }
  500. err = mptscsih_qcmd(SCpnt,done);
  501. #ifdef DMPT_DEBUG_FC
  502. if (unlikely(err)) {
  503. dfcprintk ((MYIOC_s_INFO_FMT
  504. "mptfc_qcmd.%d: %d:%d, mptscsih_qcmd returns non-zero, (%x).\n",
  505. ((MPT_SCSI_HOST *) SCpnt->device->host->hostdata)->ioc->name,
  506. ((MPT_SCSI_HOST *) SCpnt->device->host->hostdata)->ioc->sh->host_no,
  507. SCpnt->device->id,SCpnt->device->lun,err));
  508. }
  509. #endif
  510. return err;
  511. }
  512. /*
  513. * mptfc_GetFcPortPage0 - Fetch FCPort config Page0.
  514. * @ioc: Pointer to MPT_ADAPTER structure
  515. * @portnum: IOC Port number
  516. *
  517. * Return: 0 for success
  518. * -ENOMEM if no memory available
  519. * -EPERM if not allowed due to ISR context
  520. * -EAGAIN if no msg frames currently available
  521. * -EFAULT for non-successful reply or no reply (timeout)
  522. * -EINVAL portnum arg out of range (hardwired to two elements)
  523. */
  524. static int
  525. mptfc_GetFcPortPage0(MPT_ADAPTER *ioc, int portnum)
  526. {
  527. ConfigPageHeader_t hdr;
  528. CONFIGPARMS cfg;
  529. FCPortPage0_t *ppage0_alloc;
  530. FCPortPage0_t *pp0dest;
  531. dma_addr_t page0_dma;
  532. int data_sz;
  533. int copy_sz;
  534. int rc;
  535. int count = 400;
  536. if (portnum > 1)
  537. return -EINVAL;
  538. /* Get FCPort Page 0 header */
  539. hdr.PageVersion = 0;
  540. hdr.PageLength = 0;
  541. hdr.PageNumber = 0;
  542. hdr.PageType = MPI_CONFIG_PAGETYPE_FC_PORT;
  543. cfg.cfghdr.hdr = &hdr;
  544. cfg.physAddr = -1;
  545. cfg.action = MPI_CONFIG_ACTION_PAGE_HEADER;
  546. cfg.dir = 0;
  547. cfg.pageAddr = portnum;
  548. cfg.timeout = 0;
  549. if ((rc = mpt_config(ioc, &cfg)) != 0)
  550. return rc;
  551. if (hdr.PageLength == 0)
  552. return 0;
  553. data_sz = hdr.PageLength * 4;
  554. rc = -ENOMEM;
  555. ppage0_alloc = (FCPortPage0_t *) pci_alloc_consistent(ioc->pcidev, data_sz, &page0_dma);
  556. if (ppage0_alloc) {
  557. try_again:
  558. memset((u8 *)ppage0_alloc, 0, data_sz);
  559. cfg.physAddr = page0_dma;
  560. cfg.action = MPI_CONFIG_ACTION_PAGE_READ_CURRENT;
  561. if ((rc = mpt_config(ioc, &cfg)) == 0) {
  562. /* save the data */
  563. pp0dest = &ioc->fc_port_page0[portnum];
  564. copy_sz = min_t(int, sizeof(FCPortPage0_t), data_sz);
  565. memcpy(pp0dest, ppage0_alloc, copy_sz);
  566. /*
  567. * Normalize endianness of structure data,
  568. * by byte-swapping all > 1 byte fields!
  569. */
  570. pp0dest->Flags = le32_to_cpu(pp0dest->Flags);
  571. pp0dest->PortIdentifier = le32_to_cpu(pp0dest->PortIdentifier);
  572. pp0dest->WWNN.Low = le32_to_cpu(pp0dest->WWNN.Low);
  573. pp0dest->WWNN.High = le32_to_cpu(pp0dest->WWNN.High);
  574. pp0dest->WWPN.Low = le32_to_cpu(pp0dest->WWPN.Low);
  575. pp0dest->WWPN.High = le32_to_cpu(pp0dest->WWPN.High);
  576. pp0dest->SupportedServiceClass = le32_to_cpu(pp0dest->SupportedServiceClass);
  577. pp0dest->SupportedSpeeds = le32_to_cpu(pp0dest->SupportedSpeeds);
  578. pp0dest->CurrentSpeed = le32_to_cpu(pp0dest->CurrentSpeed);
  579. pp0dest->MaxFrameSize = le32_to_cpu(pp0dest->MaxFrameSize);
  580. pp0dest->FabricWWNN.Low = le32_to_cpu(pp0dest->FabricWWNN.Low);
  581. pp0dest->FabricWWNN.High = le32_to_cpu(pp0dest->FabricWWNN.High);
  582. pp0dest->FabricWWPN.Low = le32_to_cpu(pp0dest->FabricWWPN.Low);
  583. pp0dest->FabricWWPN.High = le32_to_cpu(pp0dest->FabricWWPN.High);
  584. pp0dest->DiscoveredPortsCount = le32_to_cpu(pp0dest->DiscoveredPortsCount);
  585. pp0dest->MaxInitiators = le32_to_cpu(pp0dest->MaxInitiators);
  586. /*
  587. * if still doing discovery,
  588. * hang loose a while until finished
  589. */
  590. if (pp0dest->PortState == MPI_FCPORTPAGE0_PORTSTATE_UNKNOWN) {
  591. if (count-- > 0) {
  592. msleep(100);
  593. goto try_again;
  594. }
  595. printk(MYIOC_s_INFO_FMT "Firmware discovery not"
  596. " complete.\n",
  597. ioc->name);
  598. }
  599. }
  600. pci_free_consistent(ioc->pcidev, data_sz, (u8 *) ppage0_alloc, page0_dma);
  601. }
  602. return rc;
  603. }
  604. static int
  605. mptfc_WriteFcPortPage1(MPT_ADAPTER *ioc, int portnum)
  606. {
  607. ConfigPageHeader_t hdr;
  608. CONFIGPARMS cfg;
  609. int rc;
  610. if (portnum > 1)
  611. return -EINVAL;
  612. if (!(ioc->fc_data.fc_port_page1[portnum].data))
  613. return -EINVAL;
  614. /* get fcport page 1 header */
  615. hdr.PageVersion = 0;
  616. hdr.PageLength = 0;
  617. hdr.PageNumber = 1;
  618. hdr.PageType = MPI_CONFIG_PAGETYPE_FC_PORT;
  619. cfg.cfghdr.hdr = &hdr;
  620. cfg.physAddr = -1;
  621. cfg.action = MPI_CONFIG_ACTION_PAGE_HEADER;
  622. cfg.dir = 0;
  623. cfg.pageAddr = portnum;
  624. cfg.timeout = 0;
  625. if ((rc = mpt_config(ioc, &cfg)) != 0)
  626. return rc;
  627. if (hdr.PageLength == 0)
  628. return -ENODEV;
  629. if (hdr.PageLength*4 != ioc->fc_data.fc_port_page1[portnum].pg_sz)
  630. return -EINVAL;
  631. cfg.physAddr = ioc->fc_data.fc_port_page1[portnum].dma;
  632. cfg.action = MPI_CONFIG_ACTION_PAGE_WRITE_CURRENT;
  633. cfg.dir = 1;
  634. rc = mpt_config(ioc, &cfg);
  635. return rc;
  636. }
  637. static int
  638. mptfc_GetFcPortPage1(MPT_ADAPTER *ioc, int portnum)
  639. {
  640. ConfigPageHeader_t hdr;
  641. CONFIGPARMS cfg;
  642. FCPortPage1_t *page1_alloc;
  643. dma_addr_t page1_dma;
  644. int data_sz;
  645. int rc;
  646. if (portnum > 1)
  647. return -EINVAL;
  648. /* get fcport page 1 header */
  649. hdr.PageVersion = 0;
  650. hdr.PageLength = 0;
  651. hdr.PageNumber = 1;
  652. hdr.PageType = MPI_CONFIG_PAGETYPE_FC_PORT;
  653. cfg.cfghdr.hdr = &hdr;
  654. cfg.physAddr = -1;
  655. cfg.action = MPI_CONFIG_ACTION_PAGE_HEADER;
  656. cfg.dir = 0;
  657. cfg.pageAddr = portnum;
  658. cfg.timeout = 0;
  659. if ((rc = mpt_config(ioc, &cfg)) != 0)
  660. return rc;
  661. if (hdr.PageLength == 0)
  662. return -ENODEV;
  663. start_over:
  664. if (ioc->fc_data.fc_port_page1[portnum].data == NULL) {
  665. data_sz = hdr.PageLength * 4;
  666. if (data_sz < sizeof(FCPortPage1_t))
  667. data_sz = sizeof(FCPortPage1_t);
  668. page1_alloc = (FCPortPage1_t *) pci_alloc_consistent(ioc->pcidev,
  669. data_sz,
  670. &page1_dma);
  671. if (!page1_alloc)
  672. return -ENOMEM;
  673. }
  674. else {
  675. page1_alloc = ioc->fc_data.fc_port_page1[portnum].data;
  676. page1_dma = ioc->fc_data.fc_port_page1[portnum].dma;
  677. data_sz = ioc->fc_data.fc_port_page1[portnum].pg_sz;
  678. if (hdr.PageLength * 4 > data_sz) {
  679. ioc->fc_data.fc_port_page1[portnum].data = NULL;
  680. pci_free_consistent(ioc->pcidev, data_sz, (u8 *)
  681. page1_alloc, page1_dma);
  682. goto start_over;
  683. }
  684. }
  685. memset(page1_alloc,0,data_sz);
  686. cfg.physAddr = page1_dma;
  687. cfg.action = MPI_CONFIG_ACTION_PAGE_READ_CURRENT;
  688. if ((rc = mpt_config(ioc, &cfg)) == 0) {
  689. ioc->fc_data.fc_port_page1[portnum].data = page1_alloc;
  690. ioc->fc_data.fc_port_page1[portnum].pg_sz = data_sz;
  691. ioc->fc_data.fc_port_page1[portnum].dma = page1_dma;
  692. }
  693. else {
  694. ioc->fc_data.fc_port_page1[portnum].data = NULL;
  695. pci_free_consistent(ioc->pcidev, data_sz, (u8 *)
  696. page1_alloc, page1_dma);
  697. }
  698. return rc;
  699. }
  700. static void
  701. mptfc_SetFcPortPage1_defaults(MPT_ADAPTER *ioc)
  702. {
  703. int ii;
  704. FCPortPage1_t *pp1;
  705. #define MPTFC_FW_DEVICE_TIMEOUT (1)
  706. #define MPTFC_FW_IO_PEND_TIMEOUT (1)
  707. #define ON_FLAGS (MPI_FCPORTPAGE1_FLAGS_IMMEDIATE_ERROR_REPLY)
  708. #define OFF_FLAGS (MPI_FCPORTPAGE1_FLAGS_VERBOSE_RESCAN_EVENTS)
  709. for (ii=0; ii<ioc->facts.NumberOfPorts; ii++) {
  710. if (mptfc_GetFcPortPage1(ioc, ii) != 0)
  711. continue;
  712. pp1 = ioc->fc_data.fc_port_page1[ii].data;
  713. if ((pp1->InitiatorDeviceTimeout == MPTFC_FW_DEVICE_TIMEOUT)
  714. && (pp1->InitiatorIoPendTimeout == MPTFC_FW_IO_PEND_TIMEOUT)
  715. && ((pp1->Flags & ON_FLAGS) == ON_FLAGS)
  716. && ((pp1->Flags & OFF_FLAGS) == 0))
  717. continue;
  718. pp1->InitiatorDeviceTimeout = MPTFC_FW_DEVICE_TIMEOUT;
  719. pp1->InitiatorIoPendTimeout = MPTFC_FW_IO_PEND_TIMEOUT;
  720. pp1->Flags &= ~OFF_FLAGS;
  721. pp1->Flags |= ON_FLAGS;
  722. mptfc_WriteFcPortPage1(ioc, ii);
  723. }
  724. }
  725. static void
  726. mptfc_init_host_attr(MPT_ADAPTER *ioc,int portnum)
  727. {
  728. unsigned class = 0, cos = 0;
  729. /* don't know what to do as only one scsi (fc) host was allocated */
  730. if (portnum != 0)
  731. return;
  732. class = ioc->fc_port_page0[portnum].SupportedServiceClass;
  733. if (class & MPI_FCPORTPAGE0_SUPPORT_CLASS_1)
  734. cos |= FC_COS_CLASS1;
  735. if (class & MPI_FCPORTPAGE0_SUPPORT_CLASS_2)
  736. cos |= FC_COS_CLASS2;
  737. if (class & MPI_FCPORTPAGE0_SUPPORT_CLASS_3)
  738. cos |= FC_COS_CLASS3;
  739. fc_host_node_name(ioc->sh) =
  740. (u64)ioc->fc_port_page0[portnum].WWNN.High << 32
  741. | (u64)ioc->fc_port_page0[portnum].WWNN.Low;
  742. fc_host_port_name(ioc->sh) =
  743. (u64)ioc->fc_port_page0[portnum].WWPN.High << 32
  744. | (u64)ioc->fc_port_page0[portnum].WWPN.Low;
  745. fc_host_port_id(ioc->sh) = ioc->fc_port_page0[portnum].PortIdentifier;
  746. fc_host_supported_classes(ioc->sh) = cos;
  747. fc_host_tgtid_bind_type(ioc->sh) = FC_TGTID_BIND_BY_WWPN;
  748. }
  749. static void
  750. mptfc_setup_reset(void *arg)
  751. {
  752. MPT_ADAPTER *ioc = (MPT_ADAPTER *)arg;
  753. u64 pn;
  754. struct mptfc_rport_info *ri;
  755. /* reset about to happen, delete (block) all rports */
  756. list_for_each_entry(ri, &ioc->fc_rports, list) {
  757. if (ri->flags & MPT_RPORT_INFO_FLAGS_REGISTERED) {
  758. ri->flags &= ~MPT_RPORT_INFO_FLAGS_REGISTERED;
  759. fc_remote_port_delete(ri->rport); /* won't sleep */
  760. ri->rport = NULL;
  761. pn = (u64)ri->pg0.WWPN.High << 32 |
  762. (u64)ri->pg0.WWPN.Low;
  763. dfcprintk ((MYIOC_s_INFO_FMT
  764. "mptfc_setup_reset.%d: %llx deleted\n",
  765. ioc->name,
  766. ioc->sh->host_no,
  767. (unsigned long long)pn));
  768. }
  769. }
  770. }
  771. static void
  772. mptfc_rescan_devices(void *arg)
  773. {
  774. MPT_ADAPTER *ioc = (MPT_ADAPTER *)arg;
  775. int ii;
  776. int work_to_do;
  777. u64 pn;
  778. unsigned long flags;
  779. struct mptfc_rport_info *ri;
  780. do {
  781. /* start by tagging all ports as missing */
  782. list_for_each_entry(ri, &ioc->fc_rports, list) {
  783. if (ri->flags & MPT_RPORT_INFO_FLAGS_REGISTERED) {
  784. ri->flags |= MPT_RPORT_INFO_FLAGS_MISSING;
  785. }
  786. }
  787. /*
  788. * now rescan devices known to adapter,
  789. * will reregister existing rports
  790. */
  791. for (ii=0; ii < ioc->facts.NumberOfPorts; ii++) {
  792. (void) mptfc_GetFcPortPage0(ioc, ii);
  793. mptfc_init_host_attr(ioc,ii); /* refresh */
  794. mptfc_GetFcDevPage0(ioc,ii,mptfc_register_dev);
  795. }
  796. /* delete devices still missing */
  797. list_for_each_entry(ri, &ioc->fc_rports, list) {
  798. /* if newly missing, delete it */
  799. if (ri->flags & MPT_RPORT_INFO_FLAGS_MISSING) {
  800. ri->flags &= ~(MPT_RPORT_INFO_FLAGS_REGISTERED|
  801. MPT_RPORT_INFO_FLAGS_MISSING);
  802. fc_remote_port_delete(ri->rport); /* won't sleep */
  803. ri->rport = NULL;
  804. pn = (u64)ri->pg0.WWPN.High << 32 |
  805. (u64)ri->pg0.WWPN.Low;
  806. dfcprintk ((MYIOC_s_INFO_FMT
  807. "mptfc_rescan.%d: %llx deleted\n",
  808. ioc->name,
  809. ioc->sh->host_no,
  810. (unsigned long long)pn));
  811. }
  812. }
  813. /*
  814. * allow multiple passes as target state
  815. * might have changed during scan
  816. */
  817. spin_lock_irqsave(&ioc->fc_rescan_work_lock, flags);
  818. if (ioc->fc_rescan_work_count > 2) /* only need one more */
  819. ioc->fc_rescan_work_count = 2;
  820. work_to_do = --ioc->fc_rescan_work_count;
  821. spin_unlock_irqrestore(&ioc->fc_rescan_work_lock, flags);
  822. } while (work_to_do);
  823. }
  824. static int
  825. mptfc_probe(struct pci_dev *pdev, const struct pci_device_id *id)
  826. {
  827. struct Scsi_Host *sh;
  828. MPT_SCSI_HOST *hd;
  829. MPT_ADAPTER *ioc;
  830. unsigned long flags;
  831. int ii;
  832. int numSGE = 0;
  833. int scale;
  834. int ioc_cap;
  835. int error=0;
  836. int r;
  837. if ((r = mpt_attach(pdev,id)) != 0)
  838. return r;
  839. ioc = pci_get_drvdata(pdev);
  840. ioc->DoneCtx = mptfcDoneCtx;
  841. ioc->TaskCtx = mptfcTaskCtx;
  842. ioc->InternalCtx = mptfcInternalCtx;
  843. /* Added sanity check on readiness of the MPT adapter.
  844. */
  845. if (ioc->last_state != MPI_IOC_STATE_OPERATIONAL) {
  846. printk(MYIOC_s_WARN_FMT
  847. "Skipping because it's not operational!\n",
  848. ioc->name);
  849. error = -ENODEV;
  850. goto out_mptfc_probe;
  851. }
  852. if (!ioc->active) {
  853. printk(MYIOC_s_WARN_FMT "Skipping because it's disabled!\n",
  854. ioc->name);
  855. error = -ENODEV;
  856. goto out_mptfc_probe;
  857. }
  858. /* Sanity check - ensure at least 1 port is INITIATOR capable
  859. */
  860. ioc_cap = 0;
  861. for (ii=0; ii < ioc->facts.NumberOfPorts; ii++) {
  862. if (ioc->pfacts[ii].ProtocolFlags &
  863. MPI_PORTFACTS_PROTOCOL_INITIATOR)
  864. ioc_cap ++;
  865. }
  866. if (!ioc_cap) {
  867. printk(MYIOC_s_WARN_FMT
  868. "Skipping ioc=%p because SCSI Initiator mode is NOT enabled!\n",
  869. ioc->name, ioc);
  870. return -ENODEV;
  871. }
  872. sh = scsi_host_alloc(&mptfc_driver_template, sizeof(MPT_SCSI_HOST));
  873. if (!sh) {
  874. printk(MYIOC_s_WARN_FMT
  875. "Unable to register controller with SCSI subsystem\n",
  876. ioc->name);
  877. error = -1;
  878. goto out_mptfc_probe;
  879. }
  880. spin_lock_init(&ioc->fc_rescan_work_lock);
  881. INIT_WORK(&ioc->fc_rescan_work, mptfc_rescan_devices,(void *)ioc);
  882. INIT_WORK(&ioc->fc_setup_reset_work, mptfc_setup_reset, (void *)ioc);
  883. spin_lock_irqsave(&ioc->FreeQlock, flags);
  884. /* Attach the SCSI Host to the IOC structure
  885. */
  886. ioc->sh = sh;
  887. sh->io_port = 0;
  888. sh->n_io_port = 0;
  889. sh->irq = 0;
  890. /* set 16 byte cdb's */
  891. sh->max_cmd_len = 16;
  892. sh->max_id = MPT_MAX_FC_DEVICES<256 ? MPT_MAX_FC_DEVICES : 255;
  893. sh->max_lun = MPT_LAST_LUN + 1;
  894. sh->max_channel = 0;
  895. sh->this_id = ioc->pfacts[0].PortSCSIID;
  896. /* Required entry.
  897. */
  898. sh->unique_id = ioc->id;
  899. /* Verify that we won't exceed the maximum
  900. * number of chain buffers
  901. * We can optimize: ZZ = req_sz/sizeof(SGE)
  902. * For 32bit SGE's:
  903. * numSGE = 1 + (ZZ-1)*(maxChain -1) + ZZ
  904. * + (req_sz - 64)/sizeof(SGE)
  905. * A slightly different algorithm is required for
  906. * 64bit SGEs.
  907. */
  908. scale = ioc->req_sz/(sizeof(dma_addr_t) + sizeof(u32));
  909. if (sizeof(dma_addr_t) == sizeof(u64)) {
  910. numSGE = (scale - 1) *
  911. (ioc->facts.MaxChainDepth-1) + scale +
  912. (ioc->req_sz - 60) / (sizeof(dma_addr_t) +
  913. sizeof(u32));
  914. } else {
  915. numSGE = 1 + (scale - 1) *
  916. (ioc->facts.MaxChainDepth-1) + scale +
  917. (ioc->req_sz - 64) / (sizeof(dma_addr_t) +
  918. sizeof(u32));
  919. }
  920. if (numSGE < sh->sg_tablesize) {
  921. /* Reset this value */
  922. dprintk((MYIOC_s_INFO_FMT
  923. "Resetting sg_tablesize to %d from %d\n",
  924. ioc->name, numSGE, sh->sg_tablesize));
  925. sh->sg_tablesize = numSGE;
  926. }
  927. spin_unlock_irqrestore(&ioc->FreeQlock, flags);
  928. hd = (MPT_SCSI_HOST *) sh->hostdata;
  929. hd->ioc = ioc;
  930. /* SCSI needs scsi_cmnd lookup table!
  931. * (with size equal to req_depth*PtrSz!)
  932. */
  933. hd->ScsiLookup = kcalloc(ioc->req_depth, sizeof(void *), GFP_ATOMIC);
  934. if (!hd->ScsiLookup) {
  935. error = -ENOMEM;
  936. goto out_mptfc_probe;
  937. }
  938. dprintk((MYIOC_s_INFO_FMT "ScsiLookup @ %p\n",
  939. ioc->name, hd->ScsiLookup));
  940. /* Allocate memory for the device structures.
  941. * A non-Null pointer at an offset
  942. * indicates a device exists.
  943. * max_id = 1 + maximum id (hosts.h)
  944. */
  945. hd->Targets = kcalloc(sh->max_id, sizeof(void *), GFP_ATOMIC);
  946. if (!hd->Targets) {
  947. error = -ENOMEM;
  948. goto out_mptfc_probe;
  949. }
  950. dprintk((KERN_INFO " vdev @ %p\n", hd->Targets));
  951. /* Clear the TM flags
  952. */
  953. hd->tmPending = 0;
  954. hd->tmState = TM_STATE_NONE;
  955. hd->resetPending = 0;
  956. hd->abortSCpnt = NULL;
  957. /* Clear the pointer used to store
  958. * single-threaded commands, i.e., those
  959. * issued during a bus scan, dv and
  960. * configuration pages.
  961. */
  962. hd->cmdPtr = NULL;
  963. /* Initialize this SCSI Hosts' timers
  964. * To use, set the timer expires field
  965. * and add_timer
  966. */
  967. init_timer(&hd->timer);
  968. hd->timer.data = (unsigned long) hd;
  969. hd->timer.function = mptscsih_timer_expired;
  970. hd->mpt_pq_filter = mpt_pq_filter;
  971. ddvprintk((MYIOC_s_INFO_FMT
  972. "mpt_pq_filter %x\n",
  973. ioc->name,
  974. mpt_pq_filter));
  975. init_waitqueue_head(&hd->scandv_waitq);
  976. hd->scandv_wait_done = 0;
  977. hd->last_queue_full = 0;
  978. sh->transportt = mptfc_transport_template;
  979. error = scsi_add_host (sh, &ioc->pcidev->dev);
  980. if(error) {
  981. dprintk((KERN_ERR MYNAM
  982. "scsi_add_host failed\n"));
  983. goto out_mptfc_probe;
  984. }
  985. /* initialize workqueue */
  986. snprintf(ioc->fc_rescan_work_q_name, KOBJ_NAME_LEN, "mptfc_wq_%d",
  987. sh->host_no);
  988. ioc->fc_rescan_work_q =
  989. create_singlethread_workqueue(ioc->fc_rescan_work_q_name);
  990. if (!ioc->fc_rescan_work_q)
  991. goto out_mptfc_probe;
  992. /*
  993. * Pre-fetch FC port WWN and stuff...
  994. * (FCPortPage0_t stuff)
  995. */
  996. for (ii=0; ii < ioc->facts.NumberOfPorts; ii++) {
  997. (void) mptfc_GetFcPortPage0(ioc, ii);
  998. }
  999. mptfc_SetFcPortPage1_defaults(ioc);
  1000. /*
  1001. * scan for rports -
  1002. * by doing it via the workqueue, some locking is eliminated
  1003. */
  1004. ioc->fc_rescan_work_count = 1;
  1005. queue_work(ioc->fc_rescan_work_q, &ioc->fc_rescan_work);
  1006. flush_workqueue(ioc->fc_rescan_work_q);
  1007. return 0;
  1008. out_mptfc_probe:
  1009. mptscsih_remove(pdev);
  1010. return error;
  1011. }
  1012. static struct pci_driver mptfc_driver = {
  1013. .name = "mptfc",
  1014. .id_table = mptfc_pci_table,
  1015. .probe = mptfc_probe,
  1016. .remove = __devexit_p(mptfc_remove),
  1017. .shutdown = mptscsih_shutdown,
  1018. #ifdef CONFIG_PM
  1019. .suspend = mptscsih_suspend,
  1020. .resume = mptscsih_resume,
  1021. #endif
  1022. };
  1023. static int
  1024. mptfc_event_process(MPT_ADAPTER *ioc, EventNotificationReply_t *pEvReply)
  1025. {
  1026. MPT_SCSI_HOST *hd;
  1027. u8 event = le32_to_cpu(pEvReply->Event) & 0xFF;
  1028. unsigned long flags;
  1029. int rc=1;
  1030. devtverboseprintk((MYIOC_s_INFO_FMT "MPT event (=%02Xh) routed to SCSI host driver!\n",
  1031. ioc->name, event));
  1032. if (ioc->sh == NULL ||
  1033. ((hd = (MPT_SCSI_HOST *)ioc->sh->hostdata) == NULL))
  1034. return 1;
  1035. switch (event) {
  1036. case MPI_EVENT_RESCAN:
  1037. spin_lock_irqsave(&ioc->fc_rescan_work_lock, flags);
  1038. if (ioc->fc_rescan_work_q) {
  1039. if (ioc->fc_rescan_work_count++ == 0) {
  1040. queue_work(ioc->fc_rescan_work_q,
  1041. &ioc->fc_rescan_work);
  1042. }
  1043. }
  1044. spin_unlock_irqrestore(&ioc->fc_rescan_work_lock, flags);
  1045. break;
  1046. default:
  1047. rc = mptscsih_event_process(ioc,pEvReply);
  1048. break;
  1049. }
  1050. return rc;
  1051. }
  1052. static int
  1053. mptfc_ioc_reset(MPT_ADAPTER *ioc, int reset_phase)
  1054. {
  1055. int rc;
  1056. unsigned long flags;
  1057. rc = mptscsih_ioc_reset(ioc,reset_phase);
  1058. if (rc == 0)
  1059. return rc;
  1060. dtmprintk((KERN_WARNING MYNAM
  1061. ": IOC %s_reset routed to FC host driver!\n",
  1062. reset_phase==MPT_IOC_SETUP_RESET ? "setup" : (
  1063. reset_phase==MPT_IOC_PRE_RESET ? "pre" : "post")));
  1064. if (reset_phase == MPT_IOC_SETUP_RESET) {
  1065. spin_lock_irqsave(&ioc->fc_rescan_work_lock, flags);
  1066. if (ioc->fc_rescan_work_q) {
  1067. queue_work(ioc->fc_rescan_work_q,
  1068. &ioc->fc_setup_reset_work);
  1069. }
  1070. spin_unlock_irqrestore(&ioc->fc_rescan_work_lock, flags);
  1071. }
  1072. else if (reset_phase == MPT_IOC_PRE_RESET) {
  1073. }
  1074. else { /* MPT_IOC_POST_RESET */
  1075. mptfc_SetFcPortPage1_defaults(ioc);
  1076. spin_lock_irqsave(&ioc->fc_rescan_work_lock, flags);
  1077. if (ioc->fc_rescan_work_q) {
  1078. if (ioc->fc_rescan_work_count++ == 0) {
  1079. queue_work(ioc->fc_rescan_work_q,
  1080. &ioc->fc_rescan_work);
  1081. }
  1082. }
  1083. spin_unlock_irqrestore(&ioc->fc_rescan_work_lock, flags);
  1084. }
  1085. return 1;
  1086. }
  1087. /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
  1088. /**
  1089. * mptfc_init - Register MPT adapter(s) as SCSI host(s) with
  1090. * linux scsi mid-layer.
  1091. *
  1092. * Returns 0 for success, non-zero for failure.
  1093. */
  1094. static int __init
  1095. mptfc_init(void)
  1096. {
  1097. int error;
  1098. show_mptmod_ver(my_NAME, my_VERSION);
  1099. /* sanity check module parameters */
  1100. if (mptfc_dev_loss_tmo <= 0)
  1101. mptfc_dev_loss_tmo = MPTFC_DEV_LOSS_TMO;
  1102. mptfc_transport_template =
  1103. fc_attach_transport(&mptfc_transport_functions);
  1104. if (!mptfc_transport_template)
  1105. return -ENODEV;
  1106. mptfcDoneCtx = mpt_register(mptscsih_io_done, MPTFC_DRIVER);
  1107. mptfcTaskCtx = mpt_register(mptscsih_taskmgmt_complete, MPTFC_DRIVER);
  1108. mptfcInternalCtx = mpt_register(mptscsih_scandv_complete, MPTFC_DRIVER);
  1109. if (mpt_event_register(mptfcDoneCtx, mptfc_event_process) == 0) {
  1110. devtverboseprintk((KERN_INFO MYNAM
  1111. ": Registered for IOC event notifications\n"));
  1112. }
  1113. if (mpt_reset_register(mptfcDoneCtx, mptfc_ioc_reset) == 0) {
  1114. dprintk((KERN_INFO MYNAM
  1115. ": Registered for IOC reset notifications\n"));
  1116. }
  1117. error = pci_register_driver(&mptfc_driver);
  1118. if (error)
  1119. fc_release_transport(mptfc_transport_template);
  1120. return error;
  1121. }
  1122. /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
  1123. /**
  1124. * mptfc_remove - Removed fc infrastructure for devices
  1125. * @pdev: Pointer to pci_dev structure
  1126. *
  1127. */
  1128. static void __devexit
  1129. mptfc_remove(struct pci_dev *pdev)
  1130. {
  1131. MPT_ADAPTER *ioc = pci_get_drvdata(pdev);
  1132. struct mptfc_rport_info *p, *n;
  1133. struct workqueue_struct *work_q;
  1134. unsigned long flags;
  1135. int ii;
  1136. /* destroy workqueue */
  1137. if ((work_q=ioc->fc_rescan_work_q)) {
  1138. spin_lock_irqsave(&ioc->fc_rescan_work_lock, flags);
  1139. ioc->fc_rescan_work_q = NULL;
  1140. spin_unlock_irqrestore(&ioc->fc_rescan_work_lock, flags);
  1141. destroy_workqueue(work_q);
  1142. }
  1143. fc_remove_host(ioc->sh);
  1144. list_for_each_entry_safe(p, n, &ioc->fc_rports, list) {
  1145. list_del(&p->list);
  1146. kfree(p);
  1147. }
  1148. for (ii=0; ii<ioc->facts.NumberOfPorts; ii++) {
  1149. if (ioc->fc_data.fc_port_page1[ii].data) {
  1150. pci_free_consistent(ioc->pcidev,
  1151. ioc->fc_data.fc_port_page1[ii].pg_sz,
  1152. (u8 *) ioc->fc_data.fc_port_page1[ii].data,
  1153. ioc->fc_data.fc_port_page1[ii].dma);
  1154. ioc->fc_data.fc_port_page1[ii].data = NULL;
  1155. }
  1156. }
  1157. mptscsih_remove(pdev);
  1158. }
  1159. /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
  1160. /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
  1161. /**
  1162. * mptfc_exit - Unregisters MPT adapter(s)
  1163. *
  1164. */
  1165. static void __exit
  1166. mptfc_exit(void)
  1167. {
  1168. pci_unregister_driver(&mptfc_driver);
  1169. fc_release_transport(mptfc_transport_template);
  1170. mpt_reset_deregister(mptfcDoneCtx);
  1171. dprintk((KERN_INFO MYNAM
  1172. ": Deregistered for IOC reset notifications\n"));
  1173. mpt_event_deregister(mptfcDoneCtx);
  1174. dprintk((KERN_INFO MYNAM
  1175. ": Deregistered for IOC event notifications\n"));
  1176. mpt_deregister(mptfcInternalCtx);
  1177. mpt_deregister(mptfcTaskCtx);
  1178. mpt_deregister(mptfcDoneCtx);
  1179. }
  1180. module_init(mptfc_init);
  1181. module_exit(mptfc_exit);