mptfc.c 40 KB

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