mptfc.c 42 KB

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