mptfc.c 41 KB

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