zfcp_scsi.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790
  1. /*
  2. * zfcp device driver
  3. *
  4. * Interface to Linux SCSI midlayer.
  5. *
  6. * Copyright IBM Corp. 2002, 2018
  7. */
  8. #define KMSG_COMPONENT "zfcp"
  9. #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
  10. #include <linux/module.h>
  11. #include <linux/types.h>
  12. #include <linux/slab.h>
  13. #include <scsi/fc/fc_fcp.h>
  14. #include <scsi/scsi_eh.h>
  15. #include <linux/atomic.h>
  16. #include "zfcp_ext.h"
  17. #include "zfcp_dbf.h"
  18. #include "zfcp_fc.h"
  19. #include "zfcp_reqlist.h"
  20. static unsigned int default_depth = 32;
  21. module_param_named(queue_depth, default_depth, uint, 0600);
  22. MODULE_PARM_DESC(queue_depth, "Default queue depth for new SCSI devices");
  23. static bool enable_dif;
  24. module_param_named(dif, enable_dif, bool, 0400);
  25. MODULE_PARM_DESC(dif, "Enable DIF/DIX data integrity support");
  26. static bool allow_lun_scan = 1;
  27. module_param(allow_lun_scan, bool, 0600);
  28. MODULE_PARM_DESC(allow_lun_scan, "For NPIV, scan and attach all storage LUNs");
  29. static void zfcp_scsi_slave_destroy(struct scsi_device *sdev)
  30. {
  31. struct zfcp_scsi_dev *zfcp_sdev = sdev_to_zfcp(sdev);
  32. /* if previous slave_alloc returned early, there is nothing to do */
  33. if (!zfcp_sdev->port)
  34. return;
  35. zfcp_erp_lun_shutdown_wait(sdev, "scssd_1");
  36. put_device(&zfcp_sdev->port->dev);
  37. }
  38. static int zfcp_scsi_slave_configure(struct scsi_device *sdp)
  39. {
  40. if (sdp->tagged_supported)
  41. scsi_change_queue_depth(sdp, default_depth);
  42. return 0;
  43. }
  44. static void zfcp_scsi_command_fail(struct scsi_cmnd *scpnt, int result)
  45. {
  46. set_host_byte(scpnt, result);
  47. zfcp_dbf_scsi_fail_send(scpnt);
  48. scpnt->scsi_done(scpnt);
  49. }
  50. static
  51. int zfcp_scsi_queuecommand(struct Scsi_Host *shost, struct scsi_cmnd *scpnt)
  52. {
  53. struct zfcp_scsi_dev *zfcp_sdev = sdev_to_zfcp(scpnt->device);
  54. struct fc_rport *rport = starget_to_rport(scsi_target(scpnt->device));
  55. int status, scsi_result, ret;
  56. /* reset the status for this request */
  57. scpnt->result = 0;
  58. scpnt->host_scribble = NULL;
  59. scsi_result = fc_remote_port_chkready(rport);
  60. if (unlikely(scsi_result)) {
  61. scpnt->result = scsi_result;
  62. zfcp_dbf_scsi_fail_send(scpnt);
  63. scpnt->scsi_done(scpnt);
  64. return 0;
  65. }
  66. status = atomic_read(&zfcp_sdev->status);
  67. if (unlikely(status & ZFCP_STATUS_COMMON_ERP_FAILED) &&
  68. !(atomic_read(&zfcp_sdev->port->status) &
  69. ZFCP_STATUS_COMMON_ERP_FAILED)) {
  70. /* only LUN access denied, but port is good
  71. * not covered by FC transport, have to fail here */
  72. zfcp_scsi_command_fail(scpnt, DID_ERROR);
  73. return 0;
  74. }
  75. if (unlikely(!(status & ZFCP_STATUS_COMMON_UNBLOCKED))) {
  76. /* This could be
  77. * call to rport_delete pending: mimic retry from
  78. * fc_remote_port_chkready until rport is BLOCKED
  79. */
  80. zfcp_scsi_command_fail(scpnt, DID_IMM_RETRY);
  81. return 0;
  82. }
  83. ret = zfcp_fsf_fcp_cmnd(scpnt);
  84. if (unlikely(ret == -EBUSY))
  85. return SCSI_MLQUEUE_DEVICE_BUSY;
  86. else if (unlikely(ret < 0))
  87. return SCSI_MLQUEUE_HOST_BUSY;
  88. return ret;
  89. }
  90. static int zfcp_scsi_slave_alloc(struct scsi_device *sdev)
  91. {
  92. struct fc_rport *rport = starget_to_rport(scsi_target(sdev));
  93. struct zfcp_adapter *adapter =
  94. (struct zfcp_adapter *) sdev->host->hostdata[0];
  95. struct zfcp_scsi_dev *zfcp_sdev = sdev_to_zfcp(sdev);
  96. struct zfcp_port *port;
  97. struct zfcp_unit *unit;
  98. int npiv = adapter->connection_features & FSF_FEATURE_NPIV_MODE;
  99. zfcp_sdev->erp_action.adapter = adapter;
  100. zfcp_sdev->erp_action.sdev = sdev;
  101. port = zfcp_get_port_by_wwpn(adapter, rport->port_name);
  102. if (!port)
  103. return -ENXIO;
  104. zfcp_sdev->erp_action.port = port;
  105. unit = zfcp_unit_find(port, zfcp_scsi_dev_lun(sdev));
  106. if (unit)
  107. put_device(&unit->dev);
  108. if (!unit && !(allow_lun_scan && npiv)) {
  109. put_device(&port->dev);
  110. return -ENXIO;
  111. }
  112. zfcp_sdev->port = port;
  113. zfcp_sdev->latencies.write.channel.min = 0xFFFFFFFF;
  114. zfcp_sdev->latencies.write.fabric.min = 0xFFFFFFFF;
  115. zfcp_sdev->latencies.read.channel.min = 0xFFFFFFFF;
  116. zfcp_sdev->latencies.read.fabric.min = 0xFFFFFFFF;
  117. zfcp_sdev->latencies.cmd.channel.min = 0xFFFFFFFF;
  118. zfcp_sdev->latencies.cmd.fabric.min = 0xFFFFFFFF;
  119. spin_lock_init(&zfcp_sdev->latencies.lock);
  120. zfcp_erp_set_lun_status(sdev, ZFCP_STATUS_COMMON_RUNNING);
  121. zfcp_erp_lun_reopen(sdev, 0, "scsla_1");
  122. zfcp_erp_wait(port->adapter);
  123. return 0;
  124. }
  125. static int zfcp_scsi_eh_abort_handler(struct scsi_cmnd *scpnt)
  126. {
  127. struct Scsi_Host *scsi_host = scpnt->device->host;
  128. struct zfcp_adapter *adapter =
  129. (struct zfcp_adapter *) scsi_host->hostdata[0];
  130. struct zfcp_fsf_req *old_req, *abrt_req;
  131. unsigned long flags;
  132. unsigned long old_reqid = (unsigned long) scpnt->host_scribble;
  133. int retval = SUCCESS, ret;
  134. int retry = 3;
  135. char *dbf_tag;
  136. /* avoid race condition between late normal completion and abort */
  137. write_lock_irqsave(&adapter->abort_lock, flags);
  138. old_req = zfcp_reqlist_find(adapter->req_list, old_reqid);
  139. if (!old_req) {
  140. write_unlock_irqrestore(&adapter->abort_lock, flags);
  141. zfcp_dbf_scsi_abort("abrt_or", scpnt, NULL);
  142. return FAILED; /* completion could be in progress */
  143. }
  144. old_req->data = NULL;
  145. /* don't access old fsf_req after releasing the abort_lock */
  146. write_unlock_irqrestore(&adapter->abort_lock, flags);
  147. while (retry--) {
  148. abrt_req = zfcp_fsf_abort_fcp_cmnd(scpnt);
  149. if (abrt_req)
  150. break;
  151. zfcp_dbf_scsi_abort("abrt_wt", scpnt, NULL);
  152. zfcp_erp_wait(adapter);
  153. ret = fc_block_scsi_eh(scpnt);
  154. if (ret) {
  155. zfcp_dbf_scsi_abort("abrt_bl", scpnt, NULL);
  156. return ret;
  157. }
  158. if (!(atomic_read(&adapter->status) &
  159. ZFCP_STATUS_COMMON_RUNNING)) {
  160. zfcp_dbf_scsi_abort("abrt_ru", scpnt, NULL);
  161. return SUCCESS;
  162. }
  163. }
  164. if (!abrt_req) {
  165. zfcp_dbf_scsi_abort("abrt_ar", scpnt, NULL);
  166. return FAILED;
  167. }
  168. wait_for_completion(&abrt_req->completion);
  169. if (abrt_req->status & ZFCP_STATUS_FSFREQ_ABORTSUCCEEDED)
  170. dbf_tag = "abrt_ok";
  171. else if (abrt_req->status & ZFCP_STATUS_FSFREQ_ABORTNOTNEEDED)
  172. dbf_tag = "abrt_nn";
  173. else {
  174. dbf_tag = "abrt_fa";
  175. retval = FAILED;
  176. }
  177. zfcp_dbf_scsi_abort(dbf_tag, scpnt, abrt_req);
  178. zfcp_fsf_req_free(abrt_req);
  179. return retval;
  180. }
  181. struct zfcp_scsi_req_filter {
  182. u8 tmf_scope;
  183. u32 lun_handle;
  184. u32 port_handle;
  185. };
  186. static void zfcp_scsi_forget_cmnd(struct zfcp_fsf_req *old_req, void *data)
  187. {
  188. struct zfcp_scsi_req_filter *filter =
  189. (struct zfcp_scsi_req_filter *)data;
  190. /* already aborted - prevent side-effects - or not a SCSI command */
  191. if (old_req->data == NULL || old_req->fsf_command != FSF_QTCB_FCP_CMND)
  192. return;
  193. /* (tmf_scope == FCP_TMF_TGT_RESET || tmf_scope == FCP_TMF_LUN_RESET) */
  194. if (old_req->qtcb->header.port_handle != filter->port_handle)
  195. return;
  196. if (filter->tmf_scope == FCP_TMF_LUN_RESET &&
  197. old_req->qtcb->header.lun_handle != filter->lun_handle)
  198. return;
  199. zfcp_dbf_scsi_nullcmnd((struct scsi_cmnd *)old_req->data, old_req);
  200. old_req->data = NULL;
  201. }
  202. static void zfcp_scsi_forget_cmnds(struct zfcp_scsi_dev *zsdev, u8 tm_flags)
  203. {
  204. struct zfcp_adapter *adapter = zsdev->port->adapter;
  205. struct zfcp_scsi_req_filter filter = {
  206. .tmf_scope = FCP_TMF_TGT_RESET,
  207. .port_handle = zsdev->port->handle,
  208. };
  209. unsigned long flags;
  210. if (tm_flags == FCP_TMF_LUN_RESET) {
  211. filter.tmf_scope = FCP_TMF_LUN_RESET;
  212. filter.lun_handle = zsdev->lun_handle;
  213. }
  214. /*
  215. * abort_lock secures against other processings - in the abort-function
  216. * and normal cmnd-handler - of (struct zfcp_fsf_req *)->data
  217. */
  218. write_lock_irqsave(&adapter->abort_lock, flags);
  219. zfcp_reqlist_apply_for_all(adapter->req_list, zfcp_scsi_forget_cmnd,
  220. &filter);
  221. write_unlock_irqrestore(&adapter->abort_lock, flags);
  222. }
  223. static int zfcp_task_mgmt_function(struct scsi_cmnd *scpnt, u8 tm_flags)
  224. {
  225. struct zfcp_scsi_dev *zfcp_sdev = sdev_to_zfcp(scpnt->device);
  226. struct zfcp_adapter *adapter = zfcp_sdev->port->adapter;
  227. struct zfcp_fsf_req *fsf_req = NULL;
  228. int retval = SUCCESS, ret;
  229. int retry = 3;
  230. while (retry--) {
  231. fsf_req = zfcp_fsf_fcp_task_mgmt(scpnt, tm_flags);
  232. if (fsf_req)
  233. break;
  234. zfcp_dbf_scsi_devreset("wait", scpnt, tm_flags, NULL);
  235. zfcp_erp_wait(adapter);
  236. ret = fc_block_scsi_eh(scpnt);
  237. if (ret) {
  238. zfcp_dbf_scsi_devreset("fiof", scpnt, tm_flags, NULL);
  239. return ret;
  240. }
  241. if (!(atomic_read(&adapter->status) &
  242. ZFCP_STATUS_COMMON_RUNNING)) {
  243. zfcp_dbf_scsi_devreset("nres", scpnt, tm_flags, NULL);
  244. return SUCCESS;
  245. }
  246. }
  247. if (!fsf_req) {
  248. zfcp_dbf_scsi_devreset("reqf", scpnt, tm_flags, NULL);
  249. return FAILED;
  250. }
  251. wait_for_completion(&fsf_req->completion);
  252. if (fsf_req->status & ZFCP_STATUS_FSFREQ_TMFUNCFAILED) {
  253. zfcp_dbf_scsi_devreset("fail", scpnt, tm_flags, fsf_req);
  254. retval = FAILED;
  255. } else {
  256. zfcp_dbf_scsi_devreset("okay", scpnt, tm_flags, fsf_req);
  257. zfcp_scsi_forget_cmnds(zfcp_sdev, tm_flags);
  258. }
  259. zfcp_fsf_req_free(fsf_req);
  260. return retval;
  261. }
  262. static int zfcp_scsi_eh_device_reset_handler(struct scsi_cmnd *scpnt)
  263. {
  264. return zfcp_task_mgmt_function(scpnt, FCP_TMF_LUN_RESET);
  265. }
  266. static int zfcp_scsi_eh_target_reset_handler(struct scsi_cmnd *scpnt)
  267. {
  268. return zfcp_task_mgmt_function(scpnt, FCP_TMF_TGT_RESET);
  269. }
  270. static int zfcp_scsi_eh_host_reset_handler(struct scsi_cmnd *scpnt)
  271. {
  272. struct zfcp_scsi_dev *zfcp_sdev = sdev_to_zfcp(scpnt->device);
  273. struct zfcp_adapter *adapter = zfcp_sdev->port->adapter;
  274. int ret = SUCCESS, fc_ret;
  275. if (!(adapter->connection_features & FSF_FEATURE_NPIV_MODE)) {
  276. zfcp_erp_port_forced_reopen_all(adapter, 0, "schrh_p");
  277. zfcp_erp_wait(adapter);
  278. }
  279. zfcp_erp_adapter_reopen(adapter, 0, "schrh_1");
  280. zfcp_erp_wait(adapter);
  281. fc_ret = fc_block_scsi_eh(scpnt);
  282. if (fc_ret)
  283. ret = fc_ret;
  284. zfcp_dbf_scsi_eh("schrh_r", adapter, ~0, ret);
  285. return ret;
  286. }
  287. struct scsi_transport_template *zfcp_scsi_transport_template;
  288. static struct scsi_host_template zfcp_scsi_host_template = {
  289. .module = THIS_MODULE,
  290. .name = "zfcp",
  291. .queuecommand = zfcp_scsi_queuecommand,
  292. .eh_abort_handler = zfcp_scsi_eh_abort_handler,
  293. .eh_device_reset_handler = zfcp_scsi_eh_device_reset_handler,
  294. .eh_target_reset_handler = zfcp_scsi_eh_target_reset_handler,
  295. .eh_host_reset_handler = zfcp_scsi_eh_host_reset_handler,
  296. .slave_alloc = zfcp_scsi_slave_alloc,
  297. .slave_configure = zfcp_scsi_slave_configure,
  298. .slave_destroy = zfcp_scsi_slave_destroy,
  299. .change_queue_depth = scsi_change_queue_depth,
  300. .proc_name = "zfcp",
  301. .can_queue = 4096,
  302. .this_id = -1,
  303. .sg_tablesize = (((QDIO_MAX_ELEMENTS_PER_BUFFER - 1)
  304. * ZFCP_QDIO_MAX_SBALS_PER_REQ) - 2),
  305. /* GCD, adjusted later */
  306. .max_sectors = (((QDIO_MAX_ELEMENTS_PER_BUFFER - 1)
  307. * ZFCP_QDIO_MAX_SBALS_PER_REQ) - 2) * 8,
  308. /* GCD, adjusted later */
  309. .dma_boundary = ZFCP_QDIO_SBALE_LEN - 1,
  310. .use_clustering = 1,
  311. .shost_attrs = zfcp_sysfs_shost_attrs,
  312. .sdev_attrs = zfcp_sysfs_sdev_attrs,
  313. .track_queue_depth = 1,
  314. };
  315. /**
  316. * zfcp_scsi_adapter_register - Register SCSI and FC host with SCSI midlayer
  317. * @adapter: The zfcp adapter to register with the SCSI midlayer
  318. */
  319. int zfcp_scsi_adapter_register(struct zfcp_adapter *adapter)
  320. {
  321. struct ccw_dev_id dev_id;
  322. if (adapter->scsi_host)
  323. return 0;
  324. ccw_device_get_id(adapter->ccw_device, &dev_id);
  325. /* register adapter as SCSI host with mid layer of SCSI stack */
  326. adapter->scsi_host = scsi_host_alloc(&zfcp_scsi_host_template,
  327. sizeof (struct zfcp_adapter *));
  328. if (!adapter->scsi_host) {
  329. dev_err(&adapter->ccw_device->dev,
  330. "Registering the FCP device with the "
  331. "SCSI stack failed\n");
  332. return -EIO;
  333. }
  334. /* tell the SCSI stack some characteristics of this adapter */
  335. adapter->scsi_host->max_id = 511;
  336. adapter->scsi_host->max_lun = 0xFFFFFFFF;
  337. adapter->scsi_host->max_channel = 0;
  338. adapter->scsi_host->unique_id = dev_id.devno;
  339. adapter->scsi_host->max_cmd_len = 16; /* in struct fcp_cmnd */
  340. adapter->scsi_host->transportt = zfcp_scsi_transport_template;
  341. adapter->scsi_host->hostdata[0] = (unsigned long) adapter;
  342. if (scsi_add_host(adapter->scsi_host, &adapter->ccw_device->dev)) {
  343. scsi_host_put(adapter->scsi_host);
  344. return -EIO;
  345. }
  346. return 0;
  347. }
  348. /**
  349. * zfcp_scsi_adapter_unregister - Unregister SCSI and FC host from SCSI midlayer
  350. * @adapter: The zfcp adapter to unregister.
  351. */
  352. void zfcp_scsi_adapter_unregister(struct zfcp_adapter *adapter)
  353. {
  354. struct Scsi_Host *shost;
  355. struct zfcp_port *port;
  356. shost = adapter->scsi_host;
  357. if (!shost)
  358. return;
  359. read_lock_irq(&adapter->port_list_lock);
  360. list_for_each_entry(port, &adapter->port_list, list)
  361. port->rport = NULL;
  362. read_unlock_irq(&adapter->port_list_lock);
  363. fc_remove_host(shost);
  364. scsi_remove_host(shost);
  365. scsi_host_put(shost);
  366. adapter->scsi_host = NULL;
  367. }
  368. static struct fc_host_statistics*
  369. zfcp_init_fc_host_stats(struct zfcp_adapter *adapter)
  370. {
  371. struct fc_host_statistics *fc_stats;
  372. if (!adapter->fc_stats) {
  373. fc_stats = kmalloc(sizeof(*fc_stats), GFP_KERNEL);
  374. if (!fc_stats)
  375. return NULL;
  376. adapter->fc_stats = fc_stats; /* freed in adapter_release */
  377. }
  378. memset(adapter->fc_stats, 0, sizeof(*adapter->fc_stats));
  379. return adapter->fc_stats;
  380. }
  381. static void zfcp_adjust_fc_host_stats(struct fc_host_statistics *fc_stats,
  382. struct fsf_qtcb_bottom_port *data,
  383. struct fsf_qtcb_bottom_port *old)
  384. {
  385. fc_stats->seconds_since_last_reset =
  386. data->seconds_since_last_reset - old->seconds_since_last_reset;
  387. fc_stats->tx_frames = data->tx_frames - old->tx_frames;
  388. fc_stats->tx_words = data->tx_words - old->tx_words;
  389. fc_stats->rx_frames = data->rx_frames - old->rx_frames;
  390. fc_stats->rx_words = data->rx_words - old->rx_words;
  391. fc_stats->lip_count = data->lip - old->lip;
  392. fc_stats->nos_count = data->nos - old->nos;
  393. fc_stats->error_frames = data->error_frames - old->error_frames;
  394. fc_stats->dumped_frames = data->dumped_frames - old->dumped_frames;
  395. fc_stats->link_failure_count = data->link_failure - old->link_failure;
  396. fc_stats->loss_of_sync_count = data->loss_of_sync - old->loss_of_sync;
  397. fc_stats->loss_of_signal_count =
  398. data->loss_of_signal - old->loss_of_signal;
  399. fc_stats->prim_seq_protocol_err_count =
  400. data->psp_error_counts - old->psp_error_counts;
  401. fc_stats->invalid_tx_word_count =
  402. data->invalid_tx_words - old->invalid_tx_words;
  403. fc_stats->invalid_crc_count = data->invalid_crcs - old->invalid_crcs;
  404. fc_stats->fcp_input_requests =
  405. data->input_requests - old->input_requests;
  406. fc_stats->fcp_output_requests =
  407. data->output_requests - old->output_requests;
  408. fc_stats->fcp_control_requests =
  409. data->control_requests - old->control_requests;
  410. fc_stats->fcp_input_megabytes = data->input_mb - old->input_mb;
  411. fc_stats->fcp_output_megabytes = data->output_mb - old->output_mb;
  412. }
  413. static void zfcp_set_fc_host_stats(struct fc_host_statistics *fc_stats,
  414. struct fsf_qtcb_bottom_port *data)
  415. {
  416. fc_stats->seconds_since_last_reset = data->seconds_since_last_reset;
  417. fc_stats->tx_frames = data->tx_frames;
  418. fc_stats->tx_words = data->tx_words;
  419. fc_stats->rx_frames = data->rx_frames;
  420. fc_stats->rx_words = data->rx_words;
  421. fc_stats->lip_count = data->lip;
  422. fc_stats->nos_count = data->nos;
  423. fc_stats->error_frames = data->error_frames;
  424. fc_stats->dumped_frames = data->dumped_frames;
  425. fc_stats->link_failure_count = data->link_failure;
  426. fc_stats->loss_of_sync_count = data->loss_of_sync;
  427. fc_stats->loss_of_signal_count = data->loss_of_signal;
  428. fc_stats->prim_seq_protocol_err_count = data->psp_error_counts;
  429. fc_stats->invalid_tx_word_count = data->invalid_tx_words;
  430. fc_stats->invalid_crc_count = data->invalid_crcs;
  431. fc_stats->fcp_input_requests = data->input_requests;
  432. fc_stats->fcp_output_requests = data->output_requests;
  433. fc_stats->fcp_control_requests = data->control_requests;
  434. fc_stats->fcp_input_megabytes = data->input_mb;
  435. fc_stats->fcp_output_megabytes = data->output_mb;
  436. }
  437. static struct fc_host_statistics *zfcp_get_fc_host_stats(struct Scsi_Host *host)
  438. {
  439. struct zfcp_adapter *adapter;
  440. struct fc_host_statistics *fc_stats;
  441. struct fsf_qtcb_bottom_port *data;
  442. int ret;
  443. adapter = (struct zfcp_adapter *)host->hostdata[0];
  444. fc_stats = zfcp_init_fc_host_stats(adapter);
  445. if (!fc_stats)
  446. return NULL;
  447. data = kzalloc(sizeof(*data), GFP_KERNEL);
  448. if (!data)
  449. return NULL;
  450. ret = zfcp_fsf_exchange_port_data_sync(adapter->qdio, data);
  451. if (ret) {
  452. kfree(data);
  453. return NULL;
  454. }
  455. if (adapter->stats_reset &&
  456. ((jiffies/HZ - adapter->stats_reset) <
  457. data->seconds_since_last_reset))
  458. zfcp_adjust_fc_host_stats(fc_stats, data,
  459. adapter->stats_reset_data);
  460. else
  461. zfcp_set_fc_host_stats(fc_stats, data);
  462. kfree(data);
  463. return fc_stats;
  464. }
  465. static void zfcp_reset_fc_host_stats(struct Scsi_Host *shost)
  466. {
  467. struct zfcp_adapter *adapter;
  468. struct fsf_qtcb_bottom_port *data;
  469. int ret;
  470. adapter = (struct zfcp_adapter *)shost->hostdata[0];
  471. data = kzalloc(sizeof(*data), GFP_KERNEL);
  472. if (!data)
  473. return;
  474. ret = zfcp_fsf_exchange_port_data_sync(adapter->qdio, data);
  475. if (ret)
  476. kfree(data);
  477. else {
  478. adapter->stats_reset = jiffies/HZ;
  479. kfree(adapter->stats_reset_data);
  480. adapter->stats_reset_data = data; /* finally freed in
  481. adapter_release */
  482. }
  483. }
  484. static void zfcp_get_host_port_state(struct Scsi_Host *shost)
  485. {
  486. struct zfcp_adapter *adapter =
  487. (struct zfcp_adapter *)shost->hostdata[0];
  488. int status = atomic_read(&adapter->status);
  489. if ((status & ZFCP_STATUS_COMMON_RUNNING) &&
  490. !(status & ZFCP_STATUS_ADAPTER_LINK_UNPLUGGED))
  491. fc_host_port_state(shost) = FC_PORTSTATE_ONLINE;
  492. else if (status & ZFCP_STATUS_ADAPTER_LINK_UNPLUGGED)
  493. fc_host_port_state(shost) = FC_PORTSTATE_LINKDOWN;
  494. else if (status & ZFCP_STATUS_COMMON_ERP_FAILED)
  495. fc_host_port_state(shost) = FC_PORTSTATE_ERROR;
  496. else
  497. fc_host_port_state(shost) = FC_PORTSTATE_UNKNOWN;
  498. }
  499. static void zfcp_set_rport_dev_loss_tmo(struct fc_rport *rport, u32 timeout)
  500. {
  501. rport->dev_loss_tmo = timeout;
  502. }
  503. /**
  504. * zfcp_scsi_terminate_rport_io - Terminate all I/O on a rport
  505. * @rport: The FC rport where to teminate I/O
  506. *
  507. * Abort all pending SCSI commands for a port by closing the
  508. * port. Using a reopen avoids a conflict with a shutdown
  509. * overwriting a reopen. The "forced" ensures that a disappeared port
  510. * is not opened again as valid due to the cached plogi data in
  511. * non-NPIV mode.
  512. */
  513. static void zfcp_scsi_terminate_rport_io(struct fc_rport *rport)
  514. {
  515. struct zfcp_port *port;
  516. struct Scsi_Host *shost = rport_to_shost(rport);
  517. struct zfcp_adapter *adapter =
  518. (struct zfcp_adapter *)shost->hostdata[0];
  519. port = zfcp_get_port_by_wwpn(adapter, rport->port_name);
  520. if (port) {
  521. zfcp_erp_port_forced_reopen(port, 0, "sctrpi1");
  522. put_device(&port->dev);
  523. } else {
  524. zfcp_erp_port_forced_no_port_dbf(
  525. "sctrpin", adapter,
  526. rport->port_name /* zfcp_scsi_rport_register */,
  527. rport->port_id /* zfcp_scsi_rport_register */);
  528. }
  529. }
  530. static void zfcp_scsi_rport_register(struct zfcp_port *port)
  531. {
  532. struct fc_rport_identifiers ids;
  533. struct fc_rport *rport;
  534. if (port->rport)
  535. return;
  536. ids.node_name = port->wwnn;
  537. ids.port_name = port->wwpn;
  538. ids.port_id = port->d_id;
  539. ids.roles = FC_RPORT_ROLE_FCP_TARGET;
  540. zfcp_dbf_rec_trig_lock("scpaddy", port->adapter, port, NULL,
  541. ZFCP_PSEUDO_ERP_ACTION_RPORT_ADD,
  542. ZFCP_PSEUDO_ERP_ACTION_RPORT_ADD);
  543. rport = fc_remote_port_add(port->adapter->scsi_host, 0, &ids);
  544. if (!rport) {
  545. dev_err(&port->adapter->ccw_device->dev,
  546. "Registering port 0x%016Lx failed\n",
  547. (unsigned long long)port->wwpn);
  548. return;
  549. }
  550. rport->maxframe_size = port->maxframe_size;
  551. rport->supported_classes = port->supported_classes;
  552. port->rport = rport;
  553. port->starget_id = rport->scsi_target_id;
  554. zfcp_unit_queue_scsi_scan(port);
  555. }
  556. static void zfcp_scsi_rport_block(struct zfcp_port *port)
  557. {
  558. struct fc_rport *rport = port->rport;
  559. if (rport) {
  560. zfcp_dbf_rec_trig_lock("scpdely", port->adapter, port, NULL,
  561. ZFCP_PSEUDO_ERP_ACTION_RPORT_DEL,
  562. ZFCP_PSEUDO_ERP_ACTION_RPORT_DEL);
  563. fc_remote_port_delete(rport);
  564. port->rport = NULL;
  565. }
  566. }
  567. void zfcp_scsi_schedule_rport_register(struct zfcp_port *port)
  568. {
  569. get_device(&port->dev);
  570. port->rport_task = RPORT_ADD;
  571. if (!queue_work(port->adapter->work_queue, &port->rport_work))
  572. put_device(&port->dev);
  573. }
  574. void zfcp_scsi_schedule_rport_block(struct zfcp_port *port)
  575. {
  576. get_device(&port->dev);
  577. port->rport_task = RPORT_DEL;
  578. if (port->rport && queue_work(port->adapter->work_queue,
  579. &port->rport_work))
  580. return;
  581. put_device(&port->dev);
  582. }
  583. void zfcp_scsi_schedule_rports_block(struct zfcp_adapter *adapter)
  584. {
  585. unsigned long flags;
  586. struct zfcp_port *port;
  587. read_lock_irqsave(&adapter->port_list_lock, flags);
  588. list_for_each_entry(port, &adapter->port_list, list)
  589. zfcp_scsi_schedule_rport_block(port);
  590. read_unlock_irqrestore(&adapter->port_list_lock, flags);
  591. }
  592. void zfcp_scsi_rport_work(struct work_struct *work)
  593. {
  594. struct zfcp_port *port = container_of(work, struct zfcp_port,
  595. rport_work);
  596. while (port->rport_task) {
  597. if (port->rport_task == RPORT_ADD) {
  598. port->rport_task = RPORT_NONE;
  599. zfcp_scsi_rport_register(port);
  600. } else {
  601. port->rport_task = RPORT_NONE;
  602. zfcp_scsi_rport_block(port);
  603. }
  604. }
  605. put_device(&port->dev);
  606. }
  607. /**
  608. * zfcp_scsi_set_prot - Configure DIF/DIX support in scsi_host
  609. * @adapter: The adapter where to configure DIF/DIX for the SCSI host
  610. */
  611. void zfcp_scsi_set_prot(struct zfcp_adapter *adapter)
  612. {
  613. unsigned int mask = 0;
  614. unsigned int data_div;
  615. struct Scsi_Host *shost = adapter->scsi_host;
  616. data_div = atomic_read(&adapter->status) &
  617. ZFCP_STATUS_ADAPTER_DATA_DIV_ENABLED;
  618. if (enable_dif &&
  619. adapter->adapter_features & FSF_FEATURE_DIF_PROT_TYPE1)
  620. mask |= SHOST_DIF_TYPE1_PROTECTION;
  621. if (enable_dif && data_div &&
  622. adapter->adapter_features & FSF_FEATURE_DIX_PROT_TCPIP) {
  623. mask |= SHOST_DIX_TYPE1_PROTECTION;
  624. scsi_host_set_guard(shost, SHOST_DIX_GUARD_IP);
  625. shost->sg_prot_tablesize = adapter->qdio->max_sbale_per_req / 2;
  626. shost->sg_tablesize = adapter->qdio->max_sbale_per_req / 2;
  627. shost->max_sectors = shost->sg_tablesize * 8;
  628. }
  629. scsi_host_set_prot(shost, mask);
  630. }
  631. /**
  632. * zfcp_scsi_dif_sense_error - Report DIF/DIX error as driver sense error
  633. * @scmd: The SCSI command to report the error for
  634. * @ascq: The ASCQ to put in the sense buffer
  635. *
  636. * See the error handling in sd_done for the sense codes used here.
  637. * Set DID_SOFT_ERROR to retry the request, if possible.
  638. */
  639. void zfcp_scsi_dif_sense_error(struct scsi_cmnd *scmd, int ascq)
  640. {
  641. scsi_build_sense_buffer(1, scmd->sense_buffer,
  642. ILLEGAL_REQUEST, 0x10, ascq);
  643. set_driver_byte(scmd, DRIVER_SENSE);
  644. scmd->result |= SAM_STAT_CHECK_CONDITION;
  645. set_host_byte(scmd, DID_SOFT_ERROR);
  646. }
  647. struct fc_function_template zfcp_transport_functions = {
  648. .show_starget_port_id = 1,
  649. .show_starget_port_name = 1,
  650. .show_starget_node_name = 1,
  651. .show_rport_supported_classes = 1,
  652. .show_rport_maxframe_size = 1,
  653. .show_rport_dev_loss_tmo = 1,
  654. .show_host_node_name = 1,
  655. .show_host_port_name = 1,
  656. .show_host_permanent_port_name = 1,
  657. .show_host_supported_classes = 1,
  658. .show_host_supported_fc4s = 1,
  659. .show_host_supported_speeds = 1,
  660. .show_host_maxframe_size = 1,
  661. .show_host_serial_number = 1,
  662. .get_fc_host_stats = zfcp_get_fc_host_stats,
  663. .reset_fc_host_stats = zfcp_reset_fc_host_stats,
  664. .set_rport_dev_loss_tmo = zfcp_set_rport_dev_loss_tmo,
  665. .get_host_port_state = zfcp_get_host_port_state,
  666. .terminate_rport_io = zfcp_scsi_terminate_rport_io,
  667. .show_host_port_state = 1,
  668. .show_host_active_fc4s = 1,
  669. .bsg_request = zfcp_fc_exec_bsg_job,
  670. .bsg_timeout = zfcp_fc_timeout_bsg_job,
  671. /* no functions registered for following dynamic attributes but
  672. directly set by LLDD */
  673. .show_host_port_type = 1,
  674. .show_host_symbolic_name = 1,
  675. .show_host_speed = 1,
  676. .show_host_port_id = 1,
  677. .dd_bsg_size = sizeof(struct zfcp_fsf_ct_els),
  678. };