mpt2sas_transport.c 58 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936
  1. /*
  2. * SAS Transport Layer for MPT (Message Passing Technology) based controllers
  3. *
  4. * This code is based on drivers/scsi/mpt2sas/mpt2_transport.c
  5. * Copyright (C) 2007-2010 LSI Corporation
  6. * (mailto:DL-MPTFusionLinux@lsi.com)
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License
  10. * as published by the Free Software Foundation; either version 2
  11. * of the License, or (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * NO WARRANTY
  19. * THE PROGRAM IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OR
  20. * CONDITIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED INCLUDING, WITHOUT
  21. * LIMITATION, ANY WARRANTIES OR CONDITIONS OF TITLE, NON-INFRINGEMENT,
  22. * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. Each Recipient is
  23. * solely responsible for determining the appropriateness of using and
  24. * distributing the Program and assumes all risks associated with its
  25. * exercise of rights under this Agreement, including but not limited to
  26. * the risks and costs of program errors, damage to or loss of data,
  27. * programs or equipment, and unavailability or interruption of operations.
  28. * DISCLAIMER OF LIABILITY
  29. * NEITHER RECIPIENT NOR ANY CONTRIBUTORS SHALL HAVE ANY LIABILITY FOR ANY
  30. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  31. * DAMAGES (INCLUDING WITHOUT LIMITATION LOST PROFITS), HOWEVER CAUSED AND
  32. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
  33. * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
  34. * USE OR DISTRIBUTION OF THE PROGRAM OR THE EXERCISE OF ANY RIGHTS GRANTED
  35. * HEREUNDER, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGES
  36. * You should have received a copy of the GNU General Public License
  37. * along with this program; if not, write to the Free Software
  38. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
  39. * USA.
  40. */
  41. #include <linux/module.h>
  42. #include <linux/kernel.h>
  43. #include <linux/init.h>
  44. #include <linux/errno.h>
  45. #include <linux/sched.h>
  46. #include <linux/workqueue.h>
  47. #include <linux/delay.h>
  48. #include <linux/pci.h>
  49. #include <linux/slab.h>
  50. #include <scsi/scsi.h>
  51. #include <scsi/scsi_cmnd.h>
  52. #include <scsi/scsi_device.h>
  53. #include <scsi/scsi_host.h>
  54. #include <scsi/scsi_transport_sas.h>
  55. #include <scsi/scsi_dbg.h>
  56. #include "mpt2sas_base.h"
  57. /**
  58. * _transport_sas_node_find_by_sas_address - sas node search
  59. * @ioc: per adapter object
  60. * @sas_address: sas address of expander or sas host
  61. * Context: Calling function should acquire ioc->sas_node_lock.
  62. *
  63. * Search for either hba phys or expander device based on handle, then returns
  64. * the sas_node object.
  65. */
  66. static struct _sas_node *
  67. _transport_sas_node_find_by_sas_address(struct MPT2SAS_ADAPTER *ioc,
  68. u64 sas_address)
  69. {
  70. if (ioc->sas_hba.sas_address == sas_address)
  71. return &ioc->sas_hba;
  72. else
  73. return mpt2sas_scsih_expander_find_by_sas_address(ioc,
  74. sas_address);
  75. }
  76. /**
  77. * _transport_convert_phy_link_rate -
  78. * @link_rate: link rate returned from mpt firmware
  79. *
  80. * Convert link_rate from mpi fusion into sas_transport form.
  81. */
  82. static enum sas_linkrate
  83. _transport_convert_phy_link_rate(u8 link_rate)
  84. {
  85. enum sas_linkrate rc;
  86. switch (link_rate) {
  87. case MPI2_SAS_NEG_LINK_RATE_1_5:
  88. rc = SAS_LINK_RATE_1_5_GBPS;
  89. break;
  90. case MPI2_SAS_NEG_LINK_RATE_3_0:
  91. rc = SAS_LINK_RATE_3_0_GBPS;
  92. break;
  93. case MPI2_SAS_NEG_LINK_RATE_6_0:
  94. rc = SAS_LINK_RATE_6_0_GBPS;
  95. break;
  96. case MPI2_SAS_NEG_LINK_RATE_PHY_DISABLED:
  97. rc = SAS_PHY_DISABLED;
  98. break;
  99. case MPI2_SAS_NEG_LINK_RATE_NEGOTIATION_FAILED:
  100. rc = SAS_LINK_RATE_FAILED;
  101. break;
  102. case MPI2_SAS_NEG_LINK_RATE_PORT_SELECTOR:
  103. rc = SAS_SATA_PORT_SELECTOR;
  104. break;
  105. case MPI2_SAS_NEG_LINK_RATE_SMP_RESET_IN_PROGRESS:
  106. rc = SAS_PHY_RESET_IN_PROGRESS;
  107. break;
  108. default:
  109. case MPI2_SAS_NEG_LINK_RATE_SATA_OOB_COMPLETE:
  110. case MPI2_SAS_NEG_LINK_RATE_UNKNOWN_LINK_RATE:
  111. rc = SAS_LINK_RATE_UNKNOWN;
  112. break;
  113. }
  114. return rc;
  115. }
  116. /**
  117. * _transport_set_identify - set identify for phys and end devices
  118. * @ioc: per adapter object
  119. * @handle: device handle
  120. * @identify: sas identify info
  121. *
  122. * Populates sas identify info.
  123. *
  124. * Returns 0 for success, non-zero for failure.
  125. */
  126. static int
  127. _transport_set_identify(struct MPT2SAS_ADAPTER *ioc, u16 handle,
  128. struct sas_identify *identify)
  129. {
  130. Mpi2SasDevicePage0_t sas_device_pg0;
  131. Mpi2ConfigReply_t mpi_reply;
  132. u32 device_info;
  133. u32 ioc_status;
  134. if (ioc->shost_recovery || ioc->pci_error_recovery) {
  135. printk(MPT2SAS_INFO_FMT "%s: host reset in progress!\n",
  136. __func__, ioc->name);
  137. return -EFAULT;
  138. }
  139. if ((mpt2sas_config_get_sas_device_pg0(ioc, &mpi_reply, &sas_device_pg0,
  140. MPI2_SAS_DEVICE_PGAD_FORM_HANDLE, handle))) {
  141. printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
  142. ioc->name, __FILE__, __LINE__, __func__);
  143. return -ENXIO;
  144. }
  145. ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
  146. MPI2_IOCSTATUS_MASK;
  147. if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
  148. printk(MPT2SAS_ERR_FMT "handle(0x%04x), ioc_status(0x%04x)"
  149. "\nfailure at %s:%d/%s()!\n", ioc->name, handle, ioc_status,
  150. __FILE__, __LINE__, __func__);
  151. return -EIO;
  152. }
  153. memset(identify, 0, sizeof(identify));
  154. device_info = le32_to_cpu(sas_device_pg0.DeviceInfo);
  155. /* sas_address */
  156. identify->sas_address = le64_to_cpu(sas_device_pg0.SASAddress);
  157. /* device_type */
  158. switch (device_info & MPI2_SAS_DEVICE_INFO_MASK_DEVICE_TYPE) {
  159. case MPI2_SAS_DEVICE_INFO_NO_DEVICE:
  160. identify->device_type = SAS_PHY_UNUSED;
  161. break;
  162. case MPI2_SAS_DEVICE_INFO_END_DEVICE:
  163. identify->device_type = SAS_END_DEVICE;
  164. break;
  165. case MPI2_SAS_DEVICE_INFO_EDGE_EXPANDER:
  166. identify->device_type = SAS_EDGE_EXPANDER_DEVICE;
  167. break;
  168. case MPI2_SAS_DEVICE_INFO_FANOUT_EXPANDER:
  169. identify->device_type = SAS_FANOUT_EXPANDER_DEVICE;
  170. break;
  171. }
  172. /* initiator_port_protocols */
  173. if (device_info & MPI2_SAS_DEVICE_INFO_SSP_INITIATOR)
  174. identify->initiator_port_protocols |= SAS_PROTOCOL_SSP;
  175. if (device_info & MPI2_SAS_DEVICE_INFO_STP_INITIATOR)
  176. identify->initiator_port_protocols |= SAS_PROTOCOL_STP;
  177. if (device_info & MPI2_SAS_DEVICE_INFO_SMP_INITIATOR)
  178. identify->initiator_port_protocols |= SAS_PROTOCOL_SMP;
  179. if (device_info & MPI2_SAS_DEVICE_INFO_SATA_HOST)
  180. identify->initiator_port_protocols |= SAS_PROTOCOL_SATA;
  181. /* target_port_protocols */
  182. if (device_info & MPI2_SAS_DEVICE_INFO_SSP_TARGET)
  183. identify->target_port_protocols |= SAS_PROTOCOL_SSP;
  184. if (device_info & MPI2_SAS_DEVICE_INFO_STP_TARGET)
  185. identify->target_port_protocols |= SAS_PROTOCOL_STP;
  186. if (device_info & MPI2_SAS_DEVICE_INFO_SMP_TARGET)
  187. identify->target_port_protocols |= SAS_PROTOCOL_SMP;
  188. if (device_info & MPI2_SAS_DEVICE_INFO_SATA_DEVICE)
  189. identify->target_port_protocols |= SAS_PROTOCOL_SATA;
  190. return 0;
  191. }
  192. /**
  193. * mpt2sas_transport_done - internal transport layer callback handler.
  194. * @ioc: per adapter object
  195. * @smid: system request message index
  196. * @msix_index: MSIX table index supplied by the OS
  197. * @reply: reply message frame(lower 32bit addr)
  198. *
  199. * Callback handler when sending internal generated transport cmds.
  200. * The callback index passed is `ioc->transport_cb_idx`
  201. *
  202. * Return 1 meaning mf should be freed from _base_interrupt
  203. * 0 means the mf is freed from this function.
  204. */
  205. u8
  206. mpt2sas_transport_done(struct MPT2SAS_ADAPTER *ioc, u16 smid, u8 msix_index,
  207. u32 reply)
  208. {
  209. MPI2DefaultReply_t *mpi_reply;
  210. mpi_reply = mpt2sas_base_get_reply_virt_addr(ioc, reply);
  211. if (ioc->transport_cmds.status == MPT2_CMD_NOT_USED)
  212. return 1;
  213. if (ioc->transport_cmds.smid != smid)
  214. return 1;
  215. ioc->transport_cmds.status |= MPT2_CMD_COMPLETE;
  216. if (mpi_reply) {
  217. memcpy(ioc->transport_cmds.reply, mpi_reply,
  218. mpi_reply->MsgLength*4);
  219. ioc->transport_cmds.status |= MPT2_CMD_REPLY_VALID;
  220. }
  221. ioc->transport_cmds.status &= ~MPT2_CMD_PENDING;
  222. complete(&ioc->transport_cmds.done);
  223. return 1;
  224. }
  225. /* report manufacture request structure */
  226. struct rep_manu_request{
  227. u8 smp_frame_type;
  228. u8 function;
  229. u8 reserved;
  230. u8 request_length;
  231. };
  232. /* report manufacture reply structure */
  233. struct rep_manu_reply{
  234. u8 smp_frame_type; /* 0x41 */
  235. u8 function; /* 0x01 */
  236. u8 function_result;
  237. u8 response_length;
  238. u16 expander_change_count;
  239. u8 reserved0[2];
  240. u8 sas_format;
  241. u8 reserved2[3];
  242. u8 vendor_id[SAS_EXPANDER_VENDOR_ID_LEN];
  243. u8 product_id[SAS_EXPANDER_PRODUCT_ID_LEN];
  244. u8 product_rev[SAS_EXPANDER_PRODUCT_REV_LEN];
  245. u8 component_vendor_id[SAS_EXPANDER_COMPONENT_VENDOR_ID_LEN];
  246. u16 component_id;
  247. u8 component_revision_id;
  248. u8 reserved3;
  249. u8 vendor_specific[8];
  250. };
  251. /**
  252. * _transport_expander_report_manufacture - obtain SMP report_manufacture
  253. * @ioc: per adapter object
  254. * @sas_address: expander sas address
  255. * @edev: the sas_expander_device object
  256. *
  257. * Fills in the sas_expander_device object when SMP port is created.
  258. *
  259. * Returns 0 for success, non-zero for failure.
  260. */
  261. static int
  262. _transport_expander_report_manufacture(struct MPT2SAS_ADAPTER *ioc,
  263. u64 sas_address, struct sas_expander_device *edev)
  264. {
  265. Mpi2SmpPassthroughRequest_t *mpi_request;
  266. Mpi2SmpPassthroughReply_t *mpi_reply;
  267. struct rep_manu_reply *manufacture_reply;
  268. struct rep_manu_request *manufacture_request;
  269. int rc;
  270. u16 smid;
  271. u32 ioc_state;
  272. unsigned long timeleft;
  273. void *psge;
  274. u32 sgl_flags;
  275. u8 issue_reset = 0;
  276. void *data_out = NULL;
  277. dma_addr_t data_out_dma;
  278. u32 sz;
  279. u64 *sas_address_le;
  280. u16 wait_state_count;
  281. if (ioc->shost_recovery || ioc->pci_error_recovery) {
  282. printk(MPT2SAS_INFO_FMT "%s: host reset in progress!\n",
  283. __func__, ioc->name);
  284. return -EFAULT;
  285. }
  286. mutex_lock(&ioc->transport_cmds.mutex);
  287. if (ioc->transport_cmds.status != MPT2_CMD_NOT_USED) {
  288. printk(MPT2SAS_ERR_FMT "%s: transport_cmds in use\n",
  289. ioc->name, __func__);
  290. rc = -EAGAIN;
  291. goto out;
  292. }
  293. ioc->transport_cmds.status = MPT2_CMD_PENDING;
  294. wait_state_count = 0;
  295. ioc_state = mpt2sas_base_get_iocstate(ioc, 1);
  296. while (ioc_state != MPI2_IOC_STATE_OPERATIONAL) {
  297. if (wait_state_count++ == 10) {
  298. printk(MPT2SAS_ERR_FMT
  299. "%s: failed due to ioc not operational\n",
  300. ioc->name, __func__);
  301. rc = -EFAULT;
  302. goto out;
  303. }
  304. ssleep(1);
  305. ioc_state = mpt2sas_base_get_iocstate(ioc, 1);
  306. printk(MPT2SAS_INFO_FMT "%s: waiting for "
  307. "operational state(count=%d)\n", ioc->name,
  308. __func__, wait_state_count);
  309. }
  310. if (wait_state_count)
  311. printk(MPT2SAS_INFO_FMT "%s: ioc is operational\n",
  312. ioc->name, __func__);
  313. smid = mpt2sas_base_get_smid(ioc, ioc->transport_cb_idx);
  314. if (!smid) {
  315. printk(MPT2SAS_ERR_FMT "%s: failed obtaining a smid\n",
  316. ioc->name, __func__);
  317. rc = -EAGAIN;
  318. goto out;
  319. }
  320. rc = 0;
  321. mpi_request = mpt2sas_base_get_msg_frame(ioc, smid);
  322. ioc->transport_cmds.smid = smid;
  323. sz = sizeof(struct rep_manu_request) + sizeof(struct rep_manu_reply);
  324. data_out = pci_alloc_consistent(ioc->pdev, sz, &data_out_dma);
  325. if (!data_out) {
  326. printk(KERN_ERR "failure at %s:%d/%s()!\n", __FILE__,
  327. __LINE__, __func__);
  328. rc = -ENOMEM;
  329. mpt2sas_base_free_smid(ioc, smid);
  330. goto out;
  331. }
  332. manufacture_request = data_out;
  333. manufacture_request->smp_frame_type = 0x40;
  334. manufacture_request->function = 1;
  335. manufacture_request->reserved = 0;
  336. manufacture_request->request_length = 0;
  337. memset(mpi_request, 0, sizeof(Mpi2SmpPassthroughRequest_t));
  338. mpi_request->Function = MPI2_FUNCTION_SMP_PASSTHROUGH;
  339. mpi_request->PhysicalPort = 0xFF;
  340. mpi_request->VF_ID = 0; /* TODO */
  341. mpi_request->VP_ID = 0;
  342. sas_address_le = (u64 *)&mpi_request->SASAddress;
  343. *sas_address_le = cpu_to_le64(sas_address);
  344. mpi_request->RequestDataLength =
  345. cpu_to_le16(sizeof(struct rep_manu_request));
  346. psge = &mpi_request->SGL;
  347. /* WRITE sgel first */
  348. sgl_flags = (MPI2_SGE_FLAGS_SIMPLE_ELEMENT |
  349. MPI2_SGE_FLAGS_END_OF_BUFFER | MPI2_SGE_FLAGS_HOST_TO_IOC);
  350. sgl_flags = sgl_flags << MPI2_SGE_FLAGS_SHIFT;
  351. ioc->base_add_sg_single(psge, sgl_flags |
  352. sizeof(struct rep_manu_request), data_out_dma);
  353. /* incr sgel */
  354. psge += ioc->sge_size;
  355. /* READ sgel last */
  356. sgl_flags = (MPI2_SGE_FLAGS_SIMPLE_ELEMENT |
  357. MPI2_SGE_FLAGS_LAST_ELEMENT | MPI2_SGE_FLAGS_END_OF_BUFFER |
  358. MPI2_SGE_FLAGS_END_OF_LIST);
  359. sgl_flags = sgl_flags << MPI2_SGE_FLAGS_SHIFT;
  360. ioc->base_add_sg_single(psge, sgl_flags |
  361. sizeof(struct rep_manu_reply), data_out_dma +
  362. sizeof(struct rep_manu_request));
  363. dtransportprintk(ioc, printk(MPT2SAS_INFO_FMT "report_manufacture - "
  364. "send to sas_addr(0x%016llx)\n", ioc->name,
  365. (unsigned long long)sas_address));
  366. mpt2sas_base_put_smid_default(ioc, smid);
  367. init_completion(&ioc->transport_cmds.done);
  368. timeleft = wait_for_completion_timeout(&ioc->transport_cmds.done,
  369. 10*HZ);
  370. if (!(ioc->transport_cmds.status & MPT2_CMD_COMPLETE)) {
  371. printk(MPT2SAS_ERR_FMT "%s: timeout\n",
  372. ioc->name, __func__);
  373. _debug_dump_mf(mpi_request,
  374. sizeof(Mpi2SmpPassthroughRequest_t)/4);
  375. if (!(ioc->transport_cmds.status & MPT2_CMD_RESET))
  376. issue_reset = 1;
  377. goto issue_host_reset;
  378. }
  379. dtransportprintk(ioc, printk(MPT2SAS_INFO_FMT "report_manufacture - "
  380. "complete\n", ioc->name));
  381. if (ioc->transport_cmds.status & MPT2_CMD_REPLY_VALID) {
  382. u8 *tmp;
  383. mpi_reply = ioc->transport_cmds.reply;
  384. dtransportprintk(ioc, printk(MPT2SAS_INFO_FMT
  385. "report_manufacture - reply data transfer size(%d)\n",
  386. ioc->name, le16_to_cpu(mpi_reply->ResponseDataLength)));
  387. if (le16_to_cpu(mpi_reply->ResponseDataLength) !=
  388. sizeof(struct rep_manu_reply))
  389. goto out;
  390. manufacture_reply = data_out + sizeof(struct rep_manu_request);
  391. strncpy(edev->vendor_id, manufacture_reply->vendor_id,
  392. SAS_EXPANDER_VENDOR_ID_LEN);
  393. strncpy(edev->product_id, manufacture_reply->product_id,
  394. SAS_EXPANDER_PRODUCT_ID_LEN);
  395. strncpy(edev->product_rev, manufacture_reply->product_rev,
  396. SAS_EXPANDER_PRODUCT_REV_LEN);
  397. edev->level = manufacture_reply->sas_format & 1;
  398. if (edev->level) {
  399. strncpy(edev->component_vendor_id,
  400. manufacture_reply->component_vendor_id,
  401. SAS_EXPANDER_COMPONENT_VENDOR_ID_LEN);
  402. tmp = (u8 *)&manufacture_reply->component_id;
  403. edev->component_id = tmp[0] << 8 | tmp[1];
  404. edev->component_revision_id =
  405. manufacture_reply->component_revision_id;
  406. }
  407. } else
  408. dtransportprintk(ioc, printk(MPT2SAS_INFO_FMT
  409. "report_manufacture - no reply\n", ioc->name));
  410. issue_host_reset:
  411. if (issue_reset)
  412. mpt2sas_base_hard_reset_handler(ioc, CAN_SLEEP,
  413. FORCE_BIG_HAMMER);
  414. out:
  415. ioc->transport_cmds.status = MPT2_CMD_NOT_USED;
  416. if (data_out)
  417. pci_free_consistent(ioc->pdev, sz, data_out, data_out_dma);
  418. mutex_unlock(&ioc->transport_cmds.mutex);
  419. return rc;
  420. }
  421. /**
  422. * _transport_delete_duplicate_port - (see below description)
  423. * @ioc: per adapter object
  424. * @sas_node: sas node object (either expander or sas host)
  425. * @sas_address: sas address of device being added
  426. * @phy_num: phy number
  427. *
  428. * This function is called when attempting to add a new port that is claiming
  429. * the same phy resources already in use by another port. If we don't release
  430. * the claimed phy resources, the sas transport layer will hang from the BUG
  431. * in sas_port_add_phy.
  432. *
  433. * The reason we would hit this issue is becuase someone is changing the
  434. * sas address of a device on the fly, meanwhile controller firmware sends
  435. * EVENTs out of order when removing the previous instance of the device.
  436. */
  437. static void
  438. _transport_delete_duplicate_port(struct MPT2SAS_ADAPTER *ioc,
  439. struct _sas_node *sas_node, u64 sas_address, int phy_num)
  440. {
  441. struct _sas_port *mpt2sas_port, *mpt2sas_port_duplicate;
  442. struct _sas_phy *mpt2sas_phy;
  443. printk(MPT2SAS_ERR_FMT "new device located at sas_addr(0x%016llx), "
  444. "phy_id(%d)\n", ioc->name, (unsigned long long)sas_address,
  445. phy_num);
  446. mpt2sas_port_duplicate = NULL;
  447. list_for_each_entry(mpt2sas_port, &sas_node->sas_port_list, port_list) {
  448. dev_printk(KERN_ERR, &mpt2sas_port->port->dev,
  449. "existing device at sas_addr(0x%016llx), num_phys(%d)\n",
  450. (unsigned long long)
  451. mpt2sas_port->remote_identify.sas_address,
  452. mpt2sas_port->num_phys);
  453. list_for_each_entry(mpt2sas_phy, &mpt2sas_port->phy_list,
  454. port_siblings) {
  455. dev_printk(KERN_ERR, &mpt2sas_phy->phy->dev,
  456. "phy_number(%d)\n", mpt2sas_phy->phy_id);
  457. if (mpt2sas_phy->phy_id == phy_num)
  458. mpt2sas_port_duplicate = mpt2sas_port;
  459. }
  460. }
  461. if (!mpt2sas_port_duplicate)
  462. return;
  463. dev_printk(KERN_ERR, &mpt2sas_port_duplicate->port->dev,
  464. "deleting duplicate device at sas_addr(0x%016llx), phy(%d)!!!!\n",
  465. (unsigned long long)
  466. mpt2sas_port_duplicate->remote_identify.sas_address, phy_num);
  467. ioc->logging_level |= MPT_DEBUG_TRANSPORT;
  468. mpt2sas_transport_port_remove(ioc,
  469. mpt2sas_port_duplicate->remote_identify.sas_address,
  470. sas_node->sas_address);
  471. ioc->logging_level &= ~MPT_DEBUG_TRANSPORT;
  472. }
  473. /**
  474. * _transport_sanity_check - sanity check when adding a new port
  475. * @ioc: per adapter object
  476. * @sas_node: sas node object (either expander or sas host)
  477. * @sas_address: sas address of device being added
  478. *
  479. * See the explanation above from _transport_delete_duplicate_port
  480. */
  481. static void
  482. _transport_sanity_check(struct MPT2SAS_ADAPTER *ioc, struct _sas_node *sas_node,
  483. u64 sas_address)
  484. {
  485. int i;
  486. for (i = 0; i < sas_node->num_phys; i++)
  487. if (sas_node->phy[i].remote_identify.sas_address == sas_address)
  488. if (sas_node->phy[i].phy_belongs_to_port)
  489. _transport_delete_duplicate_port(ioc, sas_node,
  490. sas_address, i);
  491. }
  492. /**
  493. * mpt2sas_transport_port_add - insert port to the list
  494. * @ioc: per adapter object
  495. * @handle: handle of attached device
  496. * @sas_address: sas address of parent expander or sas host
  497. * Context: This function will acquire ioc->sas_node_lock.
  498. *
  499. * Adding new port object to the sas_node->sas_port_list.
  500. *
  501. * Returns mpt2sas_port.
  502. */
  503. struct _sas_port *
  504. mpt2sas_transport_port_add(struct MPT2SAS_ADAPTER *ioc, u16 handle,
  505. u64 sas_address)
  506. {
  507. struct _sas_phy *mpt2sas_phy, *next;
  508. struct _sas_port *mpt2sas_port;
  509. unsigned long flags;
  510. struct _sas_node *sas_node;
  511. struct sas_rphy *rphy;
  512. int i;
  513. struct sas_port *port;
  514. mpt2sas_port = kzalloc(sizeof(struct _sas_port),
  515. GFP_KERNEL);
  516. if (!mpt2sas_port) {
  517. printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
  518. ioc->name, __FILE__, __LINE__, __func__);
  519. return NULL;
  520. }
  521. INIT_LIST_HEAD(&mpt2sas_port->port_list);
  522. INIT_LIST_HEAD(&mpt2sas_port->phy_list);
  523. spin_lock_irqsave(&ioc->sas_node_lock, flags);
  524. sas_node = _transport_sas_node_find_by_sas_address(ioc, sas_address);
  525. spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
  526. if (!sas_node) {
  527. printk(MPT2SAS_ERR_FMT "%s: Could not find "
  528. "parent sas_address(0x%016llx)!\n", ioc->name,
  529. __func__, (unsigned long long)sas_address);
  530. goto out_fail;
  531. }
  532. if ((_transport_set_identify(ioc, handle,
  533. &mpt2sas_port->remote_identify))) {
  534. printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
  535. ioc->name, __FILE__, __LINE__, __func__);
  536. goto out_fail;
  537. }
  538. if (mpt2sas_port->remote_identify.device_type == SAS_PHY_UNUSED) {
  539. printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
  540. ioc->name, __FILE__, __LINE__, __func__);
  541. goto out_fail;
  542. }
  543. _transport_sanity_check(ioc, sas_node,
  544. mpt2sas_port->remote_identify.sas_address);
  545. for (i = 0; i < sas_node->num_phys; i++) {
  546. if (sas_node->phy[i].remote_identify.sas_address !=
  547. mpt2sas_port->remote_identify.sas_address)
  548. continue;
  549. list_add_tail(&sas_node->phy[i].port_siblings,
  550. &mpt2sas_port->phy_list);
  551. mpt2sas_port->num_phys++;
  552. }
  553. if (!mpt2sas_port->num_phys) {
  554. printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
  555. ioc->name, __FILE__, __LINE__, __func__);
  556. goto out_fail;
  557. }
  558. port = sas_port_alloc_num(sas_node->parent_dev);
  559. if ((sas_port_add(port))) {
  560. printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
  561. ioc->name, __FILE__, __LINE__, __func__);
  562. goto out_fail;
  563. }
  564. list_for_each_entry(mpt2sas_phy, &mpt2sas_port->phy_list,
  565. port_siblings) {
  566. if ((ioc->logging_level & MPT_DEBUG_TRANSPORT))
  567. dev_printk(KERN_INFO, &port->dev, "add: handle(0x%04x)"
  568. ", sas_addr(0x%016llx), phy(%d)\n", handle,
  569. (unsigned long long)
  570. mpt2sas_port->remote_identify.sas_address,
  571. mpt2sas_phy->phy_id);
  572. sas_port_add_phy(port, mpt2sas_phy->phy);
  573. mpt2sas_phy->phy_belongs_to_port = 1;
  574. }
  575. mpt2sas_port->port = port;
  576. if (mpt2sas_port->remote_identify.device_type == SAS_END_DEVICE)
  577. rphy = sas_end_device_alloc(port);
  578. else
  579. rphy = sas_expander_alloc(port,
  580. mpt2sas_port->remote_identify.device_type);
  581. rphy->identify = mpt2sas_port->remote_identify;
  582. if ((sas_rphy_add(rphy))) {
  583. printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
  584. ioc->name, __FILE__, __LINE__, __func__);
  585. }
  586. if ((ioc->logging_level & MPT_DEBUG_TRANSPORT))
  587. dev_printk(KERN_INFO, &rphy->dev, "add: handle(0x%04x), "
  588. "sas_addr(0x%016llx)\n", handle,
  589. (unsigned long long)
  590. mpt2sas_port->remote_identify.sas_address);
  591. mpt2sas_port->rphy = rphy;
  592. spin_lock_irqsave(&ioc->sas_node_lock, flags);
  593. list_add_tail(&mpt2sas_port->port_list, &sas_node->sas_port_list);
  594. spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
  595. /* fill in report manufacture */
  596. if (mpt2sas_port->remote_identify.device_type ==
  597. MPI2_SAS_DEVICE_INFO_EDGE_EXPANDER ||
  598. mpt2sas_port->remote_identify.device_type ==
  599. MPI2_SAS_DEVICE_INFO_FANOUT_EXPANDER)
  600. _transport_expander_report_manufacture(ioc,
  601. mpt2sas_port->remote_identify.sas_address,
  602. rphy_to_expander_device(rphy));
  603. return mpt2sas_port;
  604. out_fail:
  605. list_for_each_entry_safe(mpt2sas_phy, next, &mpt2sas_port->phy_list,
  606. port_siblings)
  607. list_del(&mpt2sas_phy->port_siblings);
  608. kfree(mpt2sas_port);
  609. return NULL;
  610. }
  611. /**
  612. * mpt2sas_transport_port_remove - remove port from the list
  613. * @ioc: per adapter object
  614. * @sas_address: sas address of attached device
  615. * @sas_address_parent: sas address of parent expander or sas host
  616. * Context: This function will acquire ioc->sas_node_lock.
  617. *
  618. * Removing object and freeing associated memory from the
  619. * ioc->sas_port_list.
  620. *
  621. * Return nothing.
  622. */
  623. void
  624. mpt2sas_transport_port_remove(struct MPT2SAS_ADAPTER *ioc, u64 sas_address,
  625. u64 sas_address_parent)
  626. {
  627. int i;
  628. unsigned long flags;
  629. struct _sas_port *mpt2sas_port, *next;
  630. struct _sas_node *sas_node;
  631. u8 found = 0;
  632. struct _sas_phy *mpt2sas_phy, *next_phy;
  633. spin_lock_irqsave(&ioc->sas_node_lock, flags);
  634. sas_node = _transport_sas_node_find_by_sas_address(ioc,
  635. sas_address_parent);
  636. spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
  637. if (!sas_node)
  638. return;
  639. list_for_each_entry_safe(mpt2sas_port, next, &sas_node->sas_port_list,
  640. port_list) {
  641. if (mpt2sas_port->remote_identify.sas_address != sas_address)
  642. continue;
  643. found = 1;
  644. list_del(&mpt2sas_port->port_list);
  645. goto out;
  646. }
  647. out:
  648. if (!found)
  649. return;
  650. for (i = 0; i < sas_node->num_phys; i++) {
  651. if (sas_node->phy[i].remote_identify.sas_address == sas_address)
  652. memset(&sas_node->phy[i].remote_identify, 0 ,
  653. sizeof(struct sas_identify));
  654. }
  655. list_for_each_entry_safe(mpt2sas_phy, next_phy,
  656. &mpt2sas_port->phy_list, port_siblings) {
  657. if ((ioc->logging_level & MPT_DEBUG_TRANSPORT))
  658. dev_printk(KERN_INFO, &mpt2sas_port->port->dev,
  659. "remove: sas_addr(0x%016llx), phy(%d)\n",
  660. (unsigned long long)
  661. mpt2sas_port->remote_identify.sas_address,
  662. mpt2sas_phy->phy_id);
  663. mpt2sas_phy->phy_belongs_to_port = 0;
  664. sas_port_delete_phy(mpt2sas_port->port, mpt2sas_phy->phy);
  665. list_del(&mpt2sas_phy->port_siblings);
  666. }
  667. sas_port_delete(mpt2sas_port->port);
  668. kfree(mpt2sas_port);
  669. }
  670. /**
  671. * mpt2sas_transport_add_host_phy - report sas_host phy to transport
  672. * @ioc: per adapter object
  673. * @mpt2sas_phy: mpt2sas per phy object
  674. * @phy_pg0: sas phy page 0
  675. * @parent_dev: parent device class object
  676. *
  677. * Returns 0 for success, non-zero for failure.
  678. */
  679. int
  680. mpt2sas_transport_add_host_phy(struct MPT2SAS_ADAPTER *ioc, struct _sas_phy
  681. *mpt2sas_phy, Mpi2SasPhyPage0_t phy_pg0, struct device *parent_dev)
  682. {
  683. struct sas_phy *phy;
  684. int phy_index = mpt2sas_phy->phy_id;
  685. INIT_LIST_HEAD(&mpt2sas_phy->port_siblings);
  686. phy = sas_phy_alloc(parent_dev, phy_index);
  687. if (!phy) {
  688. printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
  689. ioc->name, __FILE__, __LINE__, __func__);
  690. return -1;
  691. }
  692. if ((_transport_set_identify(ioc, mpt2sas_phy->handle,
  693. &mpt2sas_phy->identify))) {
  694. printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
  695. ioc->name, __FILE__, __LINE__, __func__);
  696. return -1;
  697. }
  698. phy->identify = mpt2sas_phy->identify;
  699. mpt2sas_phy->attached_handle = le16_to_cpu(phy_pg0.AttachedDevHandle);
  700. if (mpt2sas_phy->attached_handle)
  701. _transport_set_identify(ioc, mpt2sas_phy->attached_handle,
  702. &mpt2sas_phy->remote_identify);
  703. phy->identify.phy_identifier = mpt2sas_phy->phy_id;
  704. phy->negotiated_linkrate = _transport_convert_phy_link_rate(
  705. phy_pg0.NegotiatedLinkRate & MPI2_SAS_NEG_LINK_RATE_MASK_PHYSICAL);
  706. phy->minimum_linkrate_hw = _transport_convert_phy_link_rate(
  707. phy_pg0.HwLinkRate & MPI2_SAS_HWRATE_MIN_RATE_MASK);
  708. phy->maximum_linkrate_hw = _transport_convert_phy_link_rate(
  709. phy_pg0.HwLinkRate >> 4);
  710. phy->minimum_linkrate = _transport_convert_phy_link_rate(
  711. phy_pg0.ProgrammedLinkRate & MPI2_SAS_PRATE_MIN_RATE_MASK);
  712. phy->maximum_linkrate = _transport_convert_phy_link_rate(
  713. phy_pg0.ProgrammedLinkRate >> 4);
  714. if ((sas_phy_add(phy))) {
  715. printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
  716. ioc->name, __FILE__, __LINE__, __func__);
  717. sas_phy_free(phy);
  718. return -1;
  719. }
  720. if ((ioc->logging_level & MPT_DEBUG_TRANSPORT))
  721. dev_printk(KERN_INFO, &phy->dev,
  722. "add: handle(0x%04x), sas_addr(0x%016llx)\n"
  723. "\tattached_handle(0x%04x), sas_addr(0x%016llx)\n",
  724. mpt2sas_phy->handle, (unsigned long long)
  725. mpt2sas_phy->identify.sas_address,
  726. mpt2sas_phy->attached_handle,
  727. (unsigned long long)
  728. mpt2sas_phy->remote_identify.sas_address);
  729. mpt2sas_phy->phy = phy;
  730. return 0;
  731. }
  732. /**
  733. * mpt2sas_transport_add_expander_phy - report expander phy to transport
  734. * @ioc: per adapter object
  735. * @mpt2sas_phy: mpt2sas per phy object
  736. * @expander_pg1: expander page 1
  737. * @parent_dev: parent device class object
  738. *
  739. * Returns 0 for success, non-zero for failure.
  740. */
  741. int
  742. mpt2sas_transport_add_expander_phy(struct MPT2SAS_ADAPTER *ioc, struct _sas_phy
  743. *mpt2sas_phy, Mpi2ExpanderPage1_t expander_pg1, struct device *parent_dev)
  744. {
  745. struct sas_phy *phy;
  746. int phy_index = mpt2sas_phy->phy_id;
  747. INIT_LIST_HEAD(&mpt2sas_phy->port_siblings);
  748. phy = sas_phy_alloc(parent_dev, phy_index);
  749. if (!phy) {
  750. printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
  751. ioc->name, __FILE__, __LINE__, __func__);
  752. return -1;
  753. }
  754. if ((_transport_set_identify(ioc, mpt2sas_phy->handle,
  755. &mpt2sas_phy->identify))) {
  756. printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
  757. ioc->name, __FILE__, __LINE__, __func__);
  758. return -1;
  759. }
  760. phy->identify = mpt2sas_phy->identify;
  761. mpt2sas_phy->attached_handle =
  762. le16_to_cpu(expander_pg1.AttachedDevHandle);
  763. if (mpt2sas_phy->attached_handle)
  764. _transport_set_identify(ioc, mpt2sas_phy->attached_handle,
  765. &mpt2sas_phy->remote_identify);
  766. phy->identify.phy_identifier = mpt2sas_phy->phy_id;
  767. phy->negotiated_linkrate = _transport_convert_phy_link_rate(
  768. expander_pg1.NegotiatedLinkRate &
  769. MPI2_SAS_NEG_LINK_RATE_MASK_PHYSICAL);
  770. phy->minimum_linkrate_hw = _transport_convert_phy_link_rate(
  771. expander_pg1.HwLinkRate & MPI2_SAS_HWRATE_MIN_RATE_MASK);
  772. phy->maximum_linkrate_hw = _transport_convert_phy_link_rate(
  773. expander_pg1.HwLinkRate >> 4);
  774. phy->minimum_linkrate = _transport_convert_phy_link_rate(
  775. expander_pg1.ProgrammedLinkRate & MPI2_SAS_PRATE_MIN_RATE_MASK);
  776. phy->maximum_linkrate = _transport_convert_phy_link_rate(
  777. expander_pg1.ProgrammedLinkRate >> 4);
  778. if ((sas_phy_add(phy))) {
  779. printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
  780. ioc->name, __FILE__, __LINE__, __func__);
  781. sas_phy_free(phy);
  782. return -1;
  783. }
  784. if ((ioc->logging_level & MPT_DEBUG_TRANSPORT))
  785. dev_printk(KERN_INFO, &phy->dev,
  786. "add: handle(0x%04x), sas_addr(0x%016llx)\n"
  787. "\tattached_handle(0x%04x), sas_addr(0x%016llx)\n",
  788. mpt2sas_phy->handle, (unsigned long long)
  789. mpt2sas_phy->identify.sas_address,
  790. mpt2sas_phy->attached_handle,
  791. (unsigned long long)
  792. mpt2sas_phy->remote_identify.sas_address);
  793. mpt2sas_phy->phy = phy;
  794. return 0;
  795. }
  796. /**
  797. * mpt2sas_transport_update_links - refreshing phy link changes
  798. * @ioc: per adapter object
  799. * @sas_address: sas address of parent expander or sas host
  800. * @handle: attached device handle
  801. * @phy_numberv: phy number
  802. * @link_rate: new link rate
  803. *
  804. * Returns nothing.
  805. */
  806. void
  807. mpt2sas_transport_update_links(struct MPT2SAS_ADAPTER *ioc,
  808. u64 sas_address, u16 handle, u8 phy_number, u8 link_rate)
  809. {
  810. unsigned long flags;
  811. struct _sas_node *sas_node;
  812. struct _sas_phy *mpt2sas_phy;
  813. if (ioc->shost_recovery || ioc->pci_error_recovery)
  814. return;
  815. spin_lock_irqsave(&ioc->sas_node_lock, flags);
  816. sas_node = _transport_sas_node_find_by_sas_address(ioc, sas_address);
  817. spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
  818. if (!sas_node)
  819. return;
  820. mpt2sas_phy = &sas_node->phy[phy_number];
  821. mpt2sas_phy->attached_handle = handle;
  822. if (handle && (link_rate >= MPI2_SAS_NEG_LINK_RATE_1_5))
  823. _transport_set_identify(ioc, handle,
  824. &mpt2sas_phy->remote_identify);
  825. else
  826. memset(&mpt2sas_phy->remote_identify, 0 , sizeof(struct
  827. sas_identify));
  828. if (mpt2sas_phy->phy)
  829. mpt2sas_phy->phy->negotiated_linkrate =
  830. _transport_convert_phy_link_rate(link_rate);
  831. if ((ioc->logging_level & MPT_DEBUG_TRANSPORT))
  832. dev_printk(KERN_INFO, &mpt2sas_phy->phy->dev,
  833. "refresh: parent sas_addr(0x%016llx),\n"
  834. "\tlink_rate(0x%02x), phy(%d)\n"
  835. "\tattached_handle(0x%04x), sas_addr(0x%016llx)\n",
  836. (unsigned long long)sas_address,
  837. link_rate, phy_number, handle, (unsigned long long)
  838. mpt2sas_phy->remote_identify.sas_address);
  839. }
  840. static inline void *
  841. phy_to_ioc(struct sas_phy *phy)
  842. {
  843. struct Scsi_Host *shost = dev_to_shost(phy->dev.parent);
  844. return shost_priv(shost);
  845. }
  846. static inline void *
  847. rphy_to_ioc(struct sas_rphy *rphy)
  848. {
  849. struct Scsi_Host *shost = dev_to_shost(rphy->dev.parent->parent);
  850. return shost_priv(shost);
  851. }
  852. /* report phy error log structure */
  853. struct phy_error_log_request{
  854. u8 smp_frame_type; /* 0x40 */
  855. u8 function; /* 0x11 */
  856. u8 allocated_response_length;
  857. u8 request_length; /* 02 */
  858. u8 reserved_1[5];
  859. u8 phy_identifier;
  860. u8 reserved_2[2];
  861. };
  862. /* report phy error log reply structure */
  863. struct phy_error_log_reply{
  864. u8 smp_frame_type; /* 0x41 */
  865. u8 function; /* 0x11 */
  866. u8 function_result;
  867. u8 response_length;
  868. u16 expander_change_count;
  869. u8 reserved_1[3];
  870. u8 phy_identifier;
  871. u8 reserved_2[2];
  872. u32 invalid_dword;
  873. u32 running_disparity_error;
  874. u32 loss_of_dword_sync;
  875. u32 phy_reset_problem;
  876. };
  877. /**
  878. * _transport_get_expander_phy_error_log - return expander counters
  879. * @ioc: per adapter object
  880. * @phy: The sas phy object
  881. *
  882. * Returns 0 for success, non-zero for failure.
  883. *
  884. */
  885. static int
  886. _transport_get_expander_phy_error_log(struct MPT2SAS_ADAPTER *ioc,
  887. struct sas_phy *phy)
  888. {
  889. Mpi2SmpPassthroughRequest_t *mpi_request;
  890. Mpi2SmpPassthroughReply_t *mpi_reply;
  891. struct phy_error_log_request *phy_error_log_request;
  892. struct phy_error_log_reply *phy_error_log_reply;
  893. int rc;
  894. u16 smid;
  895. u32 ioc_state;
  896. unsigned long timeleft;
  897. void *psge;
  898. u32 sgl_flags;
  899. u8 issue_reset = 0;
  900. void *data_out = NULL;
  901. dma_addr_t data_out_dma;
  902. u32 sz;
  903. u64 *sas_address_le;
  904. u16 wait_state_count;
  905. if (ioc->shost_recovery || ioc->pci_error_recovery) {
  906. printk(MPT2SAS_INFO_FMT "%s: host reset in progress!\n",
  907. __func__, ioc->name);
  908. return -EFAULT;
  909. }
  910. mutex_lock(&ioc->transport_cmds.mutex);
  911. if (ioc->transport_cmds.status != MPT2_CMD_NOT_USED) {
  912. printk(MPT2SAS_ERR_FMT "%s: transport_cmds in use\n",
  913. ioc->name, __func__);
  914. rc = -EAGAIN;
  915. goto out;
  916. }
  917. ioc->transport_cmds.status = MPT2_CMD_PENDING;
  918. wait_state_count = 0;
  919. ioc_state = mpt2sas_base_get_iocstate(ioc, 1);
  920. while (ioc_state != MPI2_IOC_STATE_OPERATIONAL) {
  921. if (wait_state_count++ == 10) {
  922. printk(MPT2SAS_ERR_FMT
  923. "%s: failed due to ioc not operational\n",
  924. ioc->name, __func__);
  925. rc = -EFAULT;
  926. goto out;
  927. }
  928. ssleep(1);
  929. ioc_state = mpt2sas_base_get_iocstate(ioc, 1);
  930. printk(MPT2SAS_INFO_FMT "%s: waiting for "
  931. "operational state(count=%d)\n", ioc->name,
  932. __func__, wait_state_count);
  933. }
  934. if (wait_state_count)
  935. printk(MPT2SAS_INFO_FMT "%s: ioc is operational\n",
  936. ioc->name, __func__);
  937. smid = mpt2sas_base_get_smid(ioc, ioc->transport_cb_idx);
  938. if (!smid) {
  939. printk(MPT2SAS_ERR_FMT "%s: failed obtaining a smid\n",
  940. ioc->name, __func__);
  941. rc = -EAGAIN;
  942. goto out;
  943. }
  944. mpi_request = mpt2sas_base_get_msg_frame(ioc, smid);
  945. ioc->transport_cmds.smid = smid;
  946. sz = sizeof(struct phy_error_log_request) +
  947. sizeof(struct phy_error_log_reply);
  948. data_out = pci_alloc_consistent(ioc->pdev, sz, &data_out_dma);
  949. if (!data_out) {
  950. printk(KERN_ERR "failure at %s:%d/%s()!\n", __FILE__,
  951. __LINE__, __func__);
  952. rc = -ENOMEM;
  953. mpt2sas_base_free_smid(ioc, smid);
  954. goto out;
  955. }
  956. rc = -EINVAL;
  957. memset(data_out, 0, sz);
  958. phy_error_log_request = data_out;
  959. phy_error_log_request->smp_frame_type = 0x40;
  960. phy_error_log_request->function = 0x11;
  961. phy_error_log_request->request_length = 2;
  962. phy_error_log_request->allocated_response_length = 0;
  963. phy_error_log_request->phy_identifier = phy->number;
  964. memset(mpi_request, 0, sizeof(Mpi2SmpPassthroughRequest_t));
  965. mpi_request->Function = MPI2_FUNCTION_SMP_PASSTHROUGH;
  966. mpi_request->PhysicalPort = 0xFF;
  967. mpi_request->VF_ID = 0; /* TODO */
  968. mpi_request->VP_ID = 0;
  969. sas_address_le = (u64 *)&mpi_request->SASAddress;
  970. *sas_address_le = cpu_to_le64(phy->identify.sas_address);
  971. mpi_request->RequestDataLength =
  972. cpu_to_le16(sizeof(struct phy_error_log_request));
  973. psge = &mpi_request->SGL;
  974. /* WRITE sgel first */
  975. sgl_flags = (MPI2_SGE_FLAGS_SIMPLE_ELEMENT |
  976. MPI2_SGE_FLAGS_END_OF_BUFFER | MPI2_SGE_FLAGS_HOST_TO_IOC);
  977. sgl_flags = sgl_flags << MPI2_SGE_FLAGS_SHIFT;
  978. ioc->base_add_sg_single(psge, sgl_flags |
  979. sizeof(struct phy_error_log_request), data_out_dma);
  980. /* incr sgel */
  981. psge += ioc->sge_size;
  982. /* READ sgel last */
  983. sgl_flags = (MPI2_SGE_FLAGS_SIMPLE_ELEMENT |
  984. MPI2_SGE_FLAGS_LAST_ELEMENT | MPI2_SGE_FLAGS_END_OF_BUFFER |
  985. MPI2_SGE_FLAGS_END_OF_LIST);
  986. sgl_flags = sgl_flags << MPI2_SGE_FLAGS_SHIFT;
  987. ioc->base_add_sg_single(psge, sgl_flags |
  988. sizeof(struct phy_error_log_reply), data_out_dma +
  989. sizeof(struct phy_error_log_request));
  990. dtransportprintk(ioc, printk(MPT2SAS_INFO_FMT "phy_error_log - "
  991. "send to sas_addr(0x%016llx), phy(%d)\n", ioc->name,
  992. (unsigned long long)phy->identify.sas_address, phy->number));
  993. mpt2sas_base_put_smid_default(ioc, smid);
  994. init_completion(&ioc->transport_cmds.done);
  995. timeleft = wait_for_completion_timeout(&ioc->transport_cmds.done,
  996. 10*HZ);
  997. if (!(ioc->transport_cmds.status & MPT2_CMD_COMPLETE)) {
  998. printk(MPT2SAS_ERR_FMT "%s: timeout\n",
  999. ioc->name, __func__);
  1000. _debug_dump_mf(mpi_request,
  1001. sizeof(Mpi2SmpPassthroughRequest_t)/4);
  1002. if (!(ioc->transport_cmds.status & MPT2_CMD_RESET))
  1003. issue_reset = 1;
  1004. goto issue_host_reset;
  1005. }
  1006. dtransportprintk(ioc, printk(MPT2SAS_INFO_FMT "phy_error_log - "
  1007. "complete\n", ioc->name));
  1008. if (ioc->transport_cmds.status & MPT2_CMD_REPLY_VALID) {
  1009. mpi_reply = ioc->transport_cmds.reply;
  1010. dtransportprintk(ioc, printk(MPT2SAS_INFO_FMT
  1011. "phy_error_log - reply data transfer size(%d)\n",
  1012. ioc->name, le16_to_cpu(mpi_reply->ResponseDataLength)));
  1013. if (le16_to_cpu(mpi_reply->ResponseDataLength) !=
  1014. sizeof(struct phy_error_log_reply))
  1015. goto out;
  1016. phy_error_log_reply = data_out +
  1017. sizeof(struct phy_error_log_request);
  1018. dtransportprintk(ioc, printk(MPT2SAS_INFO_FMT
  1019. "phy_error_log - function_result(%d)\n",
  1020. ioc->name, phy_error_log_reply->function_result));
  1021. phy->invalid_dword_count =
  1022. be32_to_cpu(phy_error_log_reply->invalid_dword);
  1023. phy->running_disparity_error_count =
  1024. be32_to_cpu(phy_error_log_reply->running_disparity_error);
  1025. phy->loss_of_dword_sync_count =
  1026. be32_to_cpu(phy_error_log_reply->loss_of_dword_sync);
  1027. phy->phy_reset_problem_count =
  1028. be32_to_cpu(phy_error_log_reply->phy_reset_problem);
  1029. rc = 0;
  1030. } else
  1031. dtransportprintk(ioc, printk(MPT2SAS_INFO_FMT
  1032. "phy_error_log - no reply\n", ioc->name));
  1033. issue_host_reset:
  1034. if (issue_reset)
  1035. mpt2sas_base_hard_reset_handler(ioc, CAN_SLEEP,
  1036. FORCE_BIG_HAMMER);
  1037. out:
  1038. ioc->transport_cmds.status = MPT2_CMD_NOT_USED;
  1039. if (data_out)
  1040. pci_free_consistent(ioc->pdev, sz, data_out, data_out_dma);
  1041. mutex_unlock(&ioc->transport_cmds.mutex);
  1042. return rc;
  1043. }
  1044. /**
  1045. * _transport_get_linkerrors - return phy counters for both hba and expanders
  1046. * @phy: The sas phy object
  1047. *
  1048. * Returns 0 for success, non-zero for failure.
  1049. *
  1050. */
  1051. static int
  1052. _transport_get_linkerrors(struct sas_phy *phy)
  1053. {
  1054. struct MPT2SAS_ADAPTER *ioc = phy_to_ioc(phy);
  1055. unsigned long flags;
  1056. Mpi2ConfigReply_t mpi_reply;
  1057. Mpi2SasPhyPage1_t phy_pg1;
  1058. spin_lock_irqsave(&ioc->sas_node_lock, flags);
  1059. if (_transport_sas_node_find_by_sas_address(ioc,
  1060. phy->identify.sas_address) == NULL) {
  1061. spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
  1062. return -EINVAL;
  1063. }
  1064. spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
  1065. if (phy->identify.sas_address != ioc->sas_hba.sas_address)
  1066. return _transport_get_expander_phy_error_log(ioc, phy);
  1067. /* get hba phy error logs */
  1068. if ((mpt2sas_config_get_phy_pg1(ioc, &mpi_reply, &phy_pg1,
  1069. phy->number))) {
  1070. printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
  1071. ioc->name, __FILE__, __LINE__, __func__);
  1072. return -ENXIO;
  1073. }
  1074. if (mpi_reply.IOCStatus || mpi_reply.IOCLogInfo)
  1075. printk(MPT2SAS_INFO_FMT "phy(%d), ioc_status"
  1076. "(0x%04x), loginfo(0x%08x)\n", ioc->name,
  1077. phy->number, le16_to_cpu(mpi_reply.IOCStatus),
  1078. le32_to_cpu(mpi_reply.IOCLogInfo));
  1079. phy->invalid_dword_count = le32_to_cpu(phy_pg1.InvalidDwordCount);
  1080. phy->running_disparity_error_count =
  1081. le32_to_cpu(phy_pg1.RunningDisparityErrorCount);
  1082. phy->loss_of_dword_sync_count =
  1083. le32_to_cpu(phy_pg1.LossDwordSynchCount);
  1084. phy->phy_reset_problem_count =
  1085. le32_to_cpu(phy_pg1.PhyResetProblemCount);
  1086. return 0;
  1087. }
  1088. /**
  1089. * _transport_get_enclosure_identifier -
  1090. * @phy: The sas phy object
  1091. *
  1092. * Obtain the enclosure logical id for an expander.
  1093. * Returns 0 for success, non-zero for failure.
  1094. */
  1095. static int
  1096. _transport_get_enclosure_identifier(struct sas_rphy *rphy, u64 *identifier)
  1097. {
  1098. struct MPT2SAS_ADAPTER *ioc = rphy_to_ioc(rphy);
  1099. struct _sas_device *sas_device;
  1100. unsigned long flags;
  1101. spin_lock_irqsave(&ioc->sas_device_lock, flags);
  1102. sas_device = mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
  1103. rphy->identify.sas_address);
  1104. spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
  1105. if (!sas_device)
  1106. return -ENXIO;
  1107. *identifier = sas_device->enclosure_logical_id;
  1108. return 0;
  1109. }
  1110. /**
  1111. * _transport_get_bay_identifier -
  1112. * @phy: The sas phy object
  1113. *
  1114. * Returns the slot id for a device that resides inside an enclosure.
  1115. */
  1116. static int
  1117. _transport_get_bay_identifier(struct sas_rphy *rphy)
  1118. {
  1119. struct MPT2SAS_ADAPTER *ioc = rphy_to_ioc(rphy);
  1120. struct _sas_device *sas_device;
  1121. unsigned long flags;
  1122. spin_lock_irqsave(&ioc->sas_device_lock, flags);
  1123. sas_device = mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
  1124. rphy->identify.sas_address);
  1125. spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
  1126. if (!sas_device)
  1127. return -ENXIO;
  1128. return sas_device->slot;
  1129. }
  1130. /* phy control request structure */
  1131. struct phy_control_request{
  1132. u8 smp_frame_type; /* 0x40 */
  1133. u8 function; /* 0x91 */
  1134. u8 allocated_response_length;
  1135. u8 request_length; /* 0x09 */
  1136. u16 expander_change_count;
  1137. u8 reserved_1[3];
  1138. u8 phy_identifier;
  1139. u8 phy_operation;
  1140. u8 reserved_2[13];
  1141. u64 attached_device_name;
  1142. u8 programmed_min_physical_link_rate;
  1143. u8 programmed_max_physical_link_rate;
  1144. u8 reserved_3[6];
  1145. };
  1146. /* phy control reply structure */
  1147. struct phy_control_reply{
  1148. u8 smp_frame_type; /* 0x41 */
  1149. u8 function; /* 0x11 */
  1150. u8 function_result;
  1151. u8 response_length;
  1152. };
  1153. #define SMP_PHY_CONTROL_LINK_RESET (0x01)
  1154. #define SMP_PHY_CONTROL_HARD_RESET (0x02)
  1155. #define SMP_PHY_CONTROL_DISABLE (0x03)
  1156. /**
  1157. * _transport_expander_phy_control - expander phy control
  1158. * @ioc: per adapter object
  1159. * @phy: The sas phy object
  1160. *
  1161. * Returns 0 for success, non-zero for failure.
  1162. *
  1163. */
  1164. static int
  1165. _transport_expander_phy_control(struct MPT2SAS_ADAPTER *ioc,
  1166. struct sas_phy *phy, u8 phy_operation)
  1167. {
  1168. Mpi2SmpPassthroughRequest_t *mpi_request;
  1169. Mpi2SmpPassthroughReply_t *mpi_reply;
  1170. struct phy_control_request *phy_control_request;
  1171. struct phy_control_reply *phy_control_reply;
  1172. int rc;
  1173. u16 smid;
  1174. u32 ioc_state;
  1175. unsigned long timeleft;
  1176. void *psge;
  1177. u32 sgl_flags;
  1178. u8 issue_reset = 0;
  1179. void *data_out = NULL;
  1180. dma_addr_t data_out_dma;
  1181. u32 sz;
  1182. u64 *sas_address_le;
  1183. u16 wait_state_count;
  1184. if (ioc->shost_recovery) {
  1185. printk(MPT2SAS_INFO_FMT "%s: host reset in progress!\n",
  1186. __func__, ioc->name);
  1187. return -EFAULT;
  1188. }
  1189. mutex_lock(&ioc->transport_cmds.mutex);
  1190. if (ioc->transport_cmds.status != MPT2_CMD_NOT_USED) {
  1191. printk(MPT2SAS_ERR_FMT "%s: transport_cmds in use\n",
  1192. ioc->name, __func__);
  1193. rc = -EAGAIN;
  1194. goto out;
  1195. }
  1196. ioc->transport_cmds.status = MPT2_CMD_PENDING;
  1197. wait_state_count = 0;
  1198. ioc_state = mpt2sas_base_get_iocstate(ioc, 1);
  1199. while (ioc_state != MPI2_IOC_STATE_OPERATIONAL) {
  1200. if (wait_state_count++ == 10) {
  1201. printk(MPT2SAS_ERR_FMT
  1202. "%s: failed due to ioc not operational\n",
  1203. ioc->name, __func__);
  1204. rc = -EFAULT;
  1205. goto out;
  1206. }
  1207. ssleep(1);
  1208. ioc_state = mpt2sas_base_get_iocstate(ioc, 1);
  1209. printk(MPT2SAS_INFO_FMT "%s: waiting for "
  1210. "operational state(count=%d)\n", ioc->name,
  1211. __func__, wait_state_count);
  1212. }
  1213. if (wait_state_count)
  1214. printk(MPT2SAS_INFO_FMT "%s: ioc is operational\n",
  1215. ioc->name, __func__);
  1216. smid = mpt2sas_base_get_smid(ioc, ioc->transport_cb_idx);
  1217. if (!smid) {
  1218. printk(MPT2SAS_ERR_FMT "%s: failed obtaining a smid\n",
  1219. ioc->name, __func__);
  1220. rc = -EAGAIN;
  1221. goto out;
  1222. }
  1223. mpi_request = mpt2sas_base_get_msg_frame(ioc, smid);
  1224. ioc->transport_cmds.smid = smid;
  1225. sz = sizeof(struct phy_control_request) +
  1226. sizeof(struct phy_control_reply);
  1227. data_out = pci_alloc_consistent(ioc->pdev, sz, &data_out_dma);
  1228. if (!data_out) {
  1229. printk(KERN_ERR "failure at %s:%d/%s()!\n", __FILE__,
  1230. __LINE__, __func__);
  1231. rc = -ENOMEM;
  1232. mpt2sas_base_free_smid(ioc, smid);
  1233. goto out;
  1234. }
  1235. rc = -EINVAL;
  1236. memset(data_out, 0, sz);
  1237. phy_control_request = data_out;
  1238. phy_control_request->smp_frame_type = 0x40;
  1239. phy_control_request->function = 0x91;
  1240. phy_control_request->request_length = 9;
  1241. phy_control_request->allocated_response_length = 0;
  1242. phy_control_request->phy_identifier = phy->number;
  1243. phy_control_request->phy_operation = phy_operation;
  1244. phy_control_request->programmed_min_physical_link_rate =
  1245. phy->minimum_linkrate << 4;
  1246. phy_control_request->programmed_max_physical_link_rate =
  1247. phy->maximum_linkrate << 4;
  1248. memset(mpi_request, 0, sizeof(Mpi2SmpPassthroughRequest_t));
  1249. mpi_request->Function = MPI2_FUNCTION_SMP_PASSTHROUGH;
  1250. mpi_request->PhysicalPort = 0xFF;
  1251. mpi_request->VF_ID = 0; /* TODO */
  1252. mpi_request->VP_ID = 0;
  1253. sas_address_le = (u64 *)&mpi_request->SASAddress;
  1254. *sas_address_le = cpu_to_le64(phy->identify.sas_address);
  1255. mpi_request->RequestDataLength =
  1256. cpu_to_le16(sizeof(struct phy_error_log_request));
  1257. psge = &mpi_request->SGL;
  1258. /* WRITE sgel first */
  1259. sgl_flags = (MPI2_SGE_FLAGS_SIMPLE_ELEMENT |
  1260. MPI2_SGE_FLAGS_END_OF_BUFFER | MPI2_SGE_FLAGS_HOST_TO_IOC);
  1261. sgl_flags = sgl_flags << MPI2_SGE_FLAGS_SHIFT;
  1262. ioc->base_add_sg_single(psge, sgl_flags |
  1263. sizeof(struct phy_control_request), data_out_dma);
  1264. /* incr sgel */
  1265. psge += ioc->sge_size;
  1266. /* READ sgel last */
  1267. sgl_flags = (MPI2_SGE_FLAGS_SIMPLE_ELEMENT |
  1268. MPI2_SGE_FLAGS_LAST_ELEMENT | MPI2_SGE_FLAGS_END_OF_BUFFER |
  1269. MPI2_SGE_FLAGS_END_OF_LIST);
  1270. sgl_flags = sgl_flags << MPI2_SGE_FLAGS_SHIFT;
  1271. ioc->base_add_sg_single(psge, sgl_flags |
  1272. sizeof(struct phy_control_reply), data_out_dma +
  1273. sizeof(struct phy_control_request));
  1274. dtransportprintk(ioc, printk(MPT2SAS_INFO_FMT "phy_control - "
  1275. "send to sas_addr(0x%016llx), phy(%d), opcode(%d)\n", ioc->name,
  1276. (unsigned long long)phy->identify.sas_address, phy->number,
  1277. phy_operation));
  1278. mpt2sas_base_put_smid_default(ioc, smid);
  1279. init_completion(&ioc->transport_cmds.done);
  1280. timeleft = wait_for_completion_timeout(&ioc->transport_cmds.done,
  1281. 10*HZ);
  1282. if (!(ioc->transport_cmds.status & MPT2_CMD_COMPLETE)) {
  1283. printk(MPT2SAS_ERR_FMT "%s: timeout\n",
  1284. ioc->name, __func__);
  1285. _debug_dump_mf(mpi_request,
  1286. sizeof(Mpi2SmpPassthroughRequest_t)/4);
  1287. if (!(ioc->transport_cmds.status & MPT2_CMD_RESET))
  1288. issue_reset = 1;
  1289. goto issue_host_reset;
  1290. }
  1291. dtransportprintk(ioc, printk(MPT2SAS_INFO_FMT "phy_control - "
  1292. "complete\n", ioc->name));
  1293. if (ioc->transport_cmds.status & MPT2_CMD_REPLY_VALID) {
  1294. mpi_reply = ioc->transport_cmds.reply;
  1295. dtransportprintk(ioc, printk(MPT2SAS_INFO_FMT
  1296. "phy_control - reply data transfer size(%d)\n",
  1297. ioc->name, le16_to_cpu(mpi_reply->ResponseDataLength)));
  1298. if (le16_to_cpu(mpi_reply->ResponseDataLength) !=
  1299. sizeof(struct phy_control_reply))
  1300. goto out;
  1301. phy_control_reply = data_out +
  1302. sizeof(struct phy_control_request);
  1303. dtransportprintk(ioc, printk(MPT2SAS_INFO_FMT
  1304. "phy_control - function_result(%d)\n",
  1305. ioc->name, phy_control_reply->function_result));
  1306. rc = 0;
  1307. } else
  1308. dtransportprintk(ioc, printk(MPT2SAS_INFO_FMT
  1309. "phy_control - no reply\n", ioc->name));
  1310. issue_host_reset:
  1311. if (issue_reset)
  1312. mpt2sas_base_hard_reset_handler(ioc, CAN_SLEEP,
  1313. FORCE_BIG_HAMMER);
  1314. out:
  1315. ioc->transport_cmds.status = MPT2_CMD_NOT_USED;
  1316. if (data_out)
  1317. pci_free_consistent(ioc->pdev, sz, data_out, data_out_dma);
  1318. mutex_unlock(&ioc->transport_cmds.mutex);
  1319. return rc;
  1320. }
  1321. /**
  1322. * _transport_phy_reset -
  1323. * @phy: The sas phy object
  1324. * @hard_reset:
  1325. *
  1326. * Returns 0 for success, non-zero for failure.
  1327. */
  1328. static int
  1329. _transport_phy_reset(struct sas_phy *phy, int hard_reset)
  1330. {
  1331. struct MPT2SAS_ADAPTER *ioc = phy_to_ioc(phy);
  1332. Mpi2SasIoUnitControlReply_t mpi_reply;
  1333. Mpi2SasIoUnitControlRequest_t mpi_request;
  1334. unsigned long flags;
  1335. spin_lock_irqsave(&ioc->sas_node_lock, flags);
  1336. if (_transport_sas_node_find_by_sas_address(ioc,
  1337. phy->identify.sas_address) == NULL) {
  1338. spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
  1339. return -EINVAL;
  1340. }
  1341. spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
  1342. /* handle expander phys */
  1343. if (phy->identify.sas_address != ioc->sas_hba.sas_address)
  1344. return _transport_expander_phy_control(ioc, phy,
  1345. (hard_reset == 1) ? SMP_PHY_CONTROL_HARD_RESET :
  1346. SMP_PHY_CONTROL_LINK_RESET);
  1347. /* handle hba phys */
  1348. memset(&mpi_request, 0, sizeof(Mpi2SasIoUnitControlReply_t));
  1349. mpi_request.Function = MPI2_FUNCTION_SAS_IO_UNIT_CONTROL;
  1350. mpi_request.Operation = hard_reset ?
  1351. MPI2_SAS_OP_PHY_HARD_RESET : MPI2_SAS_OP_PHY_LINK_RESET;
  1352. mpi_request.PhyNum = phy->number;
  1353. if ((mpt2sas_base_sas_iounit_control(ioc, &mpi_reply, &mpi_request))) {
  1354. printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
  1355. ioc->name, __FILE__, __LINE__, __func__);
  1356. return -ENXIO;
  1357. }
  1358. if (mpi_reply.IOCStatus || mpi_reply.IOCLogInfo)
  1359. printk(MPT2SAS_INFO_FMT "phy(%d), ioc_status"
  1360. "(0x%04x), loginfo(0x%08x)\n", ioc->name,
  1361. phy->number, le16_to_cpu(mpi_reply.IOCStatus),
  1362. le32_to_cpu(mpi_reply.IOCLogInfo));
  1363. return 0;
  1364. }
  1365. /**
  1366. * _transport_phy_enable - enable/disable phys
  1367. * @phy: The sas phy object
  1368. * @enable: enable phy when true
  1369. *
  1370. * Only support sas_host direct attached phys.
  1371. * Returns 0 for success, non-zero for failure.
  1372. */
  1373. static int
  1374. _transport_phy_enable(struct sas_phy *phy, int enable)
  1375. {
  1376. struct MPT2SAS_ADAPTER *ioc = phy_to_ioc(phy);
  1377. Mpi2SasIOUnitPage1_t *sas_iounit_pg1 = NULL;
  1378. Mpi2ConfigReply_t mpi_reply;
  1379. u16 ioc_status;
  1380. u16 sz;
  1381. int rc = 0;
  1382. unsigned long flags;
  1383. spin_lock_irqsave(&ioc->sas_node_lock, flags);
  1384. if (_transport_sas_node_find_by_sas_address(ioc,
  1385. phy->identify.sas_address) == NULL) {
  1386. spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
  1387. return -EINVAL;
  1388. }
  1389. spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
  1390. /* handle expander phys */
  1391. if (phy->identify.sas_address != ioc->sas_hba.sas_address)
  1392. return _transport_expander_phy_control(ioc, phy,
  1393. (enable == 1) ? SMP_PHY_CONTROL_LINK_RESET :
  1394. SMP_PHY_CONTROL_DISABLE);
  1395. /* handle hba phys */
  1396. /* sas_iounit page 1 */
  1397. sz = offsetof(Mpi2SasIOUnitPage1_t, PhyData) + (ioc->sas_hba.num_phys *
  1398. sizeof(Mpi2SasIOUnit1PhyData_t));
  1399. sas_iounit_pg1 = kzalloc(sz, GFP_KERNEL);
  1400. if (!sas_iounit_pg1) {
  1401. printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
  1402. ioc->name, __FILE__, __LINE__, __func__);
  1403. rc = -ENOMEM;
  1404. goto out;
  1405. }
  1406. if ((mpt2sas_config_get_sas_iounit_pg1(ioc, &mpi_reply,
  1407. sas_iounit_pg1, sz))) {
  1408. printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
  1409. ioc->name, __FILE__, __LINE__, __func__);
  1410. rc = -ENXIO;
  1411. goto out;
  1412. }
  1413. ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
  1414. MPI2_IOCSTATUS_MASK;
  1415. if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
  1416. printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
  1417. ioc->name, __FILE__, __LINE__, __func__);
  1418. rc = -EIO;
  1419. goto out;
  1420. }
  1421. if (enable)
  1422. sas_iounit_pg1->PhyData[phy->number].PhyFlags
  1423. &= ~MPI2_SASIOUNIT1_PHYFLAGS_PHY_DISABLE;
  1424. else
  1425. sas_iounit_pg1->PhyData[phy->number].PhyFlags
  1426. |= MPI2_SASIOUNIT1_PHYFLAGS_PHY_DISABLE;
  1427. mpt2sas_config_set_sas_iounit_pg1(ioc, &mpi_reply, sas_iounit_pg1, sz);
  1428. /* link reset */
  1429. if (enable)
  1430. _transport_phy_reset(phy, 0);
  1431. out:
  1432. kfree(sas_iounit_pg1);
  1433. return rc;
  1434. }
  1435. /**
  1436. * _transport_phy_speed - set phy min/max link rates
  1437. * @phy: The sas phy object
  1438. * @rates: rates defined in sas_phy_linkrates
  1439. *
  1440. * Only support sas_host direct attached phys.
  1441. * Returns 0 for success, non-zero for failure.
  1442. */
  1443. static int
  1444. _transport_phy_speed(struct sas_phy *phy, struct sas_phy_linkrates *rates)
  1445. {
  1446. struct MPT2SAS_ADAPTER *ioc = phy_to_ioc(phy);
  1447. Mpi2SasIOUnitPage1_t *sas_iounit_pg1 = NULL;
  1448. Mpi2SasPhyPage0_t phy_pg0;
  1449. Mpi2ConfigReply_t mpi_reply;
  1450. u16 ioc_status;
  1451. u16 sz;
  1452. int i;
  1453. int rc = 0;
  1454. unsigned long flags;
  1455. spin_lock_irqsave(&ioc->sas_node_lock, flags);
  1456. if (_transport_sas_node_find_by_sas_address(ioc,
  1457. phy->identify.sas_address) == NULL) {
  1458. spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
  1459. return -EINVAL;
  1460. }
  1461. spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
  1462. if (!rates->minimum_linkrate)
  1463. rates->minimum_linkrate = phy->minimum_linkrate;
  1464. else if (rates->minimum_linkrate < phy->minimum_linkrate_hw)
  1465. rates->minimum_linkrate = phy->minimum_linkrate_hw;
  1466. if (!rates->maximum_linkrate)
  1467. rates->maximum_linkrate = phy->maximum_linkrate;
  1468. else if (rates->maximum_linkrate > phy->maximum_linkrate_hw)
  1469. rates->maximum_linkrate = phy->maximum_linkrate_hw;
  1470. /* handle expander phys */
  1471. if (phy->identify.sas_address != ioc->sas_hba.sas_address) {
  1472. phy->minimum_linkrate = rates->minimum_linkrate;
  1473. phy->maximum_linkrate = rates->maximum_linkrate;
  1474. return _transport_expander_phy_control(ioc, phy,
  1475. SMP_PHY_CONTROL_LINK_RESET);
  1476. }
  1477. /* handle hba phys */
  1478. /* sas_iounit page 1 */
  1479. sz = offsetof(Mpi2SasIOUnitPage1_t, PhyData) + (ioc->sas_hba.num_phys *
  1480. sizeof(Mpi2SasIOUnit1PhyData_t));
  1481. sas_iounit_pg1 = kzalloc(sz, GFP_KERNEL);
  1482. if (!sas_iounit_pg1) {
  1483. printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
  1484. ioc->name, __FILE__, __LINE__, __func__);
  1485. rc = -ENOMEM;
  1486. goto out;
  1487. }
  1488. if ((mpt2sas_config_get_sas_iounit_pg1(ioc, &mpi_reply,
  1489. sas_iounit_pg1, sz))) {
  1490. printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
  1491. ioc->name, __FILE__, __LINE__, __func__);
  1492. rc = -ENXIO;
  1493. goto out;
  1494. }
  1495. ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
  1496. MPI2_IOCSTATUS_MASK;
  1497. if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
  1498. printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
  1499. ioc->name, __FILE__, __LINE__, __func__);
  1500. rc = -EIO;
  1501. goto out;
  1502. }
  1503. for (i = 0; i < ioc->sas_hba.num_phys; i++) {
  1504. if (phy->number != i) {
  1505. sas_iounit_pg1->PhyData[i].MaxMinLinkRate =
  1506. (ioc->sas_hba.phy[i].phy->minimum_linkrate +
  1507. (ioc->sas_hba.phy[i].phy->maximum_linkrate << 4));
  1508. } else {
  1509. sas_iounit_pg1->PhyData[i].MaxMinLinkRate =
  1510. (rates->minimum_linkrate +
  1511. (rates->maximum_linkrate << 4));
  1512. }
  1513. }
  1514. if (mpt2sas_config_set_sas_iounit_pg1(ioc, &mpi_reply, sas_iounit_pg1,
  1515. sz)) {
  1516. printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
  1517. ioc->name, __FILE__, __LINE__, __func__);
  1518. rc = -ENXIO;
  1519. goto out;
  1520. }
  1521. /* link reset */
  1522. _transport_phy_reset(phy, 0);
  1523. /* read phy page 0, then update the rates in the sas transport phy */
  1524. if (!mpt2sas_config_get_phy_pg0(ioc, &mpi_reply, &phy_pg0,
  1525. phy->number)) {
  1526. phy->minimum_linkrate = _transport_convert_phy_link_rate(
  1527. phy_pg0.ProgrammedLinkRate & MPI2_SAS_PRATE_MIN_RATE_MASK);
  1528. phy->maximum_linkrate = _transport_convert_phy_link_rate(
  1529. phy_pg0.ProgrammedLinkRate >> 4);
  1530. phy->negotiated_linkrate = _transport_convert_phy_link_rate(
  1531. phy_pg0.NegotiatedLinkRate &
  1532. MPI2_SAS_NEG_LINK_RATE_MASK_PHYSICAL);
  1533. }
  1534. out:
  1535. kfree(sas_iounit_pg1);
  1536. return rc;
  1537. }
  1538. /**
  1539. * _transport_smp_handler - transport portal for smp passthru
  1540. * @shost: shost object
  1541. * @rphy: sas transport rphy object
  1542. * @req:
  1543. *
  1544. * This used primarily for smp_utils.
  1545. * Example:
  1546. * smp_rep_general /sys/class/bsg/expander-5:0
  1547. */
  1548. static int
  1549. _transport_smp_handler(struct Scsi_Host *shost, struct sas_rphy *rphy,
  1550. struct request *req)
  1551. {
  1552. struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
  1553. Mpi2SmpPassthroughRequest_t *mpi_request;
  1554. Mpi2SmpPassthroughReply_t *mpi_reply;
  1555. int rc;
  1556. u16 smid;
  1557. u32 ioc_state;
  1558. unsigned long timeleft;
  1559. void *psge;
  1560. u32 sgl_flags;
  1561. u8 issue_reset = 0;
  1562. dma_addr_t dma_addr_in = 0;
  1563. dma_addr_t dma_addr_out = 0;
  1564. u16 wait_state_count;
  1565. struct request *rsp = req->next_rq;
  1566. if (!rsp) {
  1567. printk(MPT2SAS_ERR_FMT "%s: the smp response space is "
  1568. "missing\n", ioc->name, __func__);
  1569. return -EINVAL;
  1570. }
  1571. /* do we need to support multiple segments? */
  1572. if (req->bio->bi_vcnt > 1 || rsp->bio->bi_vcnt > 1) {
  1573. printk(MPT2SAS_ERR_FMT "%s: multiple segments req %u %u, "
  1574. "rsp %u %u\n", ioc->name, __func__, req->bio->bi_vcnt,
  1575. blk_rq_bytes(req), rsp->bio->bi_vcnt, blk_rq_bytes(rsp));
  1576. return -EINVAL;
  1577. }
  1578. if (ioc->shost_recovery) {
  1579. printk(MPT2SAS_INFO_FMT "%s: host reset in progress!\n",
  1580. __func__, ioc->name);
  1581. return -EFAULT;
  1582. }
  1583. rc = mutex_lock_interruptible(&ioc->transport_cmds.mutex);
  1584. if (rc)
  1585. return rc;
  1586. if (ioc->transport_cmds.status != MPT2_CMD_NOT_USED) {
  1587. printk(MPT2SAS_ERR_FMT "%s: transport_cmds in use\n", ioc->name,
  1588. __func__);
  1589. rc = -EAGAIN;
  1590. goto out;
  1591. }
  1592. ioc->transport_cmds.status = MPT2_CMD_PENDING;
  1593. wait_state_count = 0;
  1594. ioc_state = mpt2sas_base_get_iocstate(ioc, 1);
  1595. while (ioc_state != MPI2_IOC_STATE_OPERATIONAL) {
  1596. if (wait_state_count++ == 10) {
  1597. printk(MPT2SAS_ERR_FMT
  1598. "%s: failed due to ioc not operational\n",
  1599. ioc->name, __func__);
  1600. rc = -EFAULT;
  1601. goto out;
  1602. }
  1603. ssleep(1);
  1604. ioc_state = mpt2sas_base_get_iocstate(ioc, 1);
  1605. printk(MPT2SAS_INFO_FMT "%s: waiting for "
  1606. "operational state(count=%d)\n", ioc->name,
  1607. __func__, wait_state_count);
  1608. }
  1609. if (wait_state_count)
  1610. printk(MPT2SAS_INFO_FMT "%s: ioc is operational\n",
  1611. ioc->name, __func__);
  1612. smid = mpt2sas_base_get_smid(ioc, ioc->transport_cb_idx);
  1613. if (!smid) {
  1614. printk(MPT2SAS_ERR_FMT "%s: failed obtaining a smid\n",
  1615. ioc->name, __func__);
  1616. rc = -EAGAIN;
  1617. goto out;
  1618. }
  1619. rc = 0;
  1620. mpi_request = mpt2sas_base_get_msg_frame(ioc, smid);
  1621. ioc->transport_cmds.smid = smid;
  1622. memset(mpi_request, 0, sizeof(Mpi2SmpPassthroughRequest_t));
  1623. mpi_request->Function = MPI2_FUNCTION_SMP_PASSTHROUGH;
  1624. mpi_request->PhysicalPort = 0xFF;
  1625. mpi_request->VF_ID = 0; /* TODO */
  1626. mpi_request->VP_ID = 0;
  1627. *((u64 *)&mpi_request->SASAddress) = (rphy) ?
  1628. cpu_to_le64(rphy->identify.sas_address) :
  1629. cpu_to_le64(ioc->sas_hba.sas_address);
  1630. mpi_request->RequestDataLength = cpu_to_le16(blk_rq_bytes(req) - 4);
  1631. psge = &mpi_request->SGL;
  1632. /* WRITE sgel first */
  1633. sgl_flags = (MPI2_SGE_FLAGS_SIMPLE_ELEMENT |
  1634. MPI2_SGE_FLAGS_END_OF_BUFFER | MPI2_SGE_FLAGS_HOST_TO_IOC);
  1635. sgl_flags = sgl_flags << MPI2_SGE_FLAGS_SHIFT;
  1636. dma_addr_out = pci_map_single(ioc->pdev, bio_data(req->bio),
  1637. blk_rq_bytes(req), PCI_DMA_BIDIRECTIONAL);
  1638. if (!dma_addr_out) {
  1639. mpt2sas_base_free_smid(ioc, smid);
  1640. goto unmap;
  1641. }
  1642. ioc->base_add_sg_single(psge, sgl_flags | (blk_rq_bytes(req) - 4),
  1643. dma_addr_out);
  1644. /* incr sgel */
  1645. psge += ioc->sge_size;
  1646. /* READ sgel last */
  1647. sgl_flags = (MPI2_SGE_FLAGS_SIMPLE_ELEMENT |
  1648. MPI2_SGE_FLAGS_LAST_ELEMENT | MPI2_SGE_FLAGS_END_OF_BUFFER |
  1649. MPI2_SGE_FLAGS_END_OF_LIST);
  1650. sgl_flags = sgl_flags << MPI2_SGE_FLAGS_SHIFT;
  1651. dma_addr_in = pci_map_single(ioc->pdev, bio_data(rsp->bio),
  1652. blk_rq_bytes(rsp), PCI_DMA_BIDIRECTIONAL);
  1653. if (!dma_addr_in) {
  1654. mpt2sas_base_free_smid(ioc, smid);
  1655. goto unmap;
  1656. }
  1657. ioc->base_add_sg_single(psge, sgl_flags | (blk_rq_bytes(rsp) + 4),
  1658. dma_addr_in);
  1659. dtransportprintk(ioc, printk(MPT2SAS_INFO_FMT "%s - "
  1660. "sending smp request\n", ioc->name, __func__));
  1661. mpt2sas_base_put_smid_default(ioc, smid);
  1662. init_completion(&ioc->transport_cmds.done);
  1663. timeleft = wait_for_completion_timeout(&ioc->transport_cmds.done,
  1664. 10*HZ);
  1665. if (!(ioc->transport_cmds.status & MPT2_CMD_COMPLETE)) {
  1666. printk(MPT2SAS_ERR_FMT "%s : timeout\n",
  1667. __func__, ioc->name);
  1668. _debug_dump_mf(mpi_request,
  1669. sizeof(Mpi2SmpPassthroughRequest_t)/4);
  1670. if (!(ioc->transport_cmds.status & MPT2_CMD_RESET))
  1671. issue_reset = 1;
  1672. goto issue_host_reset;
  1673. }
  1674. dtransportprintk(ioc, printk(MPT2SAS_INFO_FMT "%s - "
  1675. "complete\n", ioc->name, __func__));
  1676. if (ioc->transport_cmds.status & MPT2_CMD_REPLY_VALID) {
  1677. mpi_reply = ioc->transport_cmds.reply;
  1678. dtransportprintk(ioc, printk(MPT2SAS_INFO_FMT
  1679. "%s - reply data transfer size(%d)\n",
  1680. ioc->name, __func__,
  1681. le16_to_cpu(mpi_reply->ResponseDataLength)));
  1682. memcpy(req->sense, mpi_reply, sizeof(*mpi_reply));
  1683. req->sense_len = sizeof(*mpi_reply);
  1684. req->resid_len = 0;
  1685. rsp->resid_len -=
  1686. le16_to_cpu(mpi_reply->ResponseDataLength);
  1687. } else {
  1688. dtransportprintk(ioc, printk(MPT2SAS_INFO_FMT
  1689. "%s - no reply\n", ioc->name, __func__));
  1690. rc = -ENXIO;
  1691. }
  1692. issue_host_reset:
  1693. if (issue_reset) {
  1694. mpt2sas_base_hard_reset_handler(ioc, CAN_SLEEP,
  1695. FORCE_BIG_HAMMER);
  1696. rc = -ETIMEDOUT;
  1697. }
  1698. unmap:
  1699. if (dma_addr_out)
  1700. pci_unmap_single(ioc->pdev, dma_addr_out, blk_rq_bytes(req),
  1701. PCI_DMA_BIDIRECTIONAL);
  1702. if (dma_addr_in)
  1703. pci_unmap_single(ioc->pdev, dma_addr_in, blk_rq_bytes(rsp),
  1704. PCI_DMA_BIDIRECTIONAL);
  1705. out:
  1706. ioc->transport_cmds.status = MPT2_CMD_NOT_USED;
  1707. mutex_unlock(&ioc->transport_cmds.mutex);
  1708. return rc;
  1709. }
  1710. struct sas_function_template mpt2sas_transport_functions = {
  1711. .get_linkerrors = _transport_get_linkerrors,
  1712. .get_enclosure_identifier = _transport_get_enclosure_identifier,
  1713. .get_bay_identifier = _transport_get_bay_identifier,
  1714. .phy_reset = _transport_phy_reset,
  1715. .phy_enable = _transport_phy_enable,
  1716. .set_phy_speed = _transport_phy_speed,
  1717. .smp_handler = _transport_smp_handler,
  1718. };
  1719. struct scsi_transport_template *mpt2sas_transport_template;