mptfc.c 42 KB

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