zfcp_scsi.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787
  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. zfcp_erp_adapter_reopen(adapter, 0, "schrh_1");
  276. zfcp_erp_wait(adapter);
  277. fc_ret = fc_block_scsi_eh(scpnt);
  278. if (fc_ret)
  279. ret = fc_ret;
  280. zfcp_dbf_scsi_eh("schrh_r", adapter, ~0, ret);
  281. return ret;
  282. }
  283. struct scsi_transport_template *zfcp_scsi_transport_template;
  284. static struct scsi_host_template zfcp_scsi_host_template = {
  285. .module = THIS_MODULE,
  286. .name = "zfcp",
  287. .queuecommand = zfcp_scsi_queuecommand,
  288. .eh_abort_handler = zfcp_scsi_eh_abort_handler,
  289. .eh_device_reset_handler = zfcp_scsi_eh_device_reset_handler,
  290. .eh_target_reset_handler = zfcp_scsi_eh_target_reset_handler,
  291. .eh_host_reset_handler = zfcp_scsi_eh_host_reset_handler,
  292. .slave_alloc = zfcp_scsi_slave_alloc,
  293. .slave_configure = zfcp_scsi_slave_configure,
  294. .slave_destroy = zfcp_scsi_slave_destroy,
  295. .change_queue_depth = scsi_change_queue_depth,
  296. .proc_name = "zfcp",
  297. .can_queue = 4096,
  298. .this_id = -1,
  299. .sg_tablesize = (((QDIO_MAX_ELEMENTS_PER_BUFFER - 1)
  300. * ZFCP_QDIO_MAX_SBALS_PER_REQ) - 2),
  301. /* GCD, adjusted later */
  302. .max_sectors = (((QDIO_MAX_ELEMENTS_PER_BUFFER - 1)
  303. * ZFCP_QDIO_MAX_SBALS_PER_REQ) - 2) * 8,
  304. /* GCD, adjusted later */
  305. .dma_boundary = ZFCP_QDIO_SBALE_LEN - 1,
  306. .use_clustering = 1,
  307. .shost_attrs = zfcp_sysfs_shost_attrs,
  308. .sdev_attrs = zfcp_sysfs_sdev_attrs,
  309. .track_queue_depth = 1,
  310. };
  311. /**
  312. * zfcp_scsi_adapter_register - Register SCSI and FC host with SCSI midlayer
  313. * @adapter: The zfcp adapter to register with the SCSI midlayer
  314. */
  315. int zfcp_scsi_adapter_register(struct zfcp_adapter *adapter)
  316. {
  317. struct ccw_dev_id dev_id;
  318. if (adapter->scsi_host)
  319. return 0;
  320. ccw_device_get_id(adapter->ccw_device, &dev_id);
  321. /* register adapter as SCSI host with mid layer of SCSI stack */
  322. adapter->scsi_host = scsi_host_alloc(&zfcp_scsi_host_template,
  323. sizeof (struct zfcp_adapter *));
  324. if (!adapter->scsi_host) {
  325. dev_err(&adapter->ccw_device->dev,
  326. "Registering the FCP device with the "
  327. "SCSI stack failed\n");
  328. return -EIO;
  329. }
  330. /* tell the SCSI stack some characteristics of this adapter */
  331. adapter->scsi_host->max_id = 511;
  332. adapter->scsi_host->max_lun = 0xFFFFFFFF;
  333. adapter->scsi_host->max_channel = 0;
  334. adapter->scsi_host->unique_id = dev_id.devno;
  335. adapter->scsi_host->max_cmd_len = 16; /* in struct fcp_cmnd */
  336. adapter->scsi_host->transportt = zfcp_scsi_transport_template;
  337. adapter->scsi_host->hostdata[0] = (unsigned long) adapter;
  338. if (scsi_add_host(adapter->scsi_host, &adapter->ccw_device->dev)) {
  339. scsi_host_put(adapter->scsi_host);
  340. return -EIO;
  341. }
  342. return 0;
  343. }
  344. /**
  345. * zfcp_scsi_adapter_unregister - Unregister SCSI and FC host from SCSI midlayer
  346. * @adapter: The zfcp adapter to unregister.
  347. */
  348. void zfcp_scsi_adapter_unregister(struct zfcp_adapter *adapter)
  349. {
  350. struct Scsi_Host *shost;
  351. struct zfcp_port *port;
  352. shost = adapter->scsi_host;
  353. if (!shost)
  354. return;
  355. read_lock_irq(&adapter->port_list_lock);
  356. list_for_each_entry(port, &adapter->port_list, list)
  357. port->rport = NULL;
  358. read_unlock_irq(&adapter->port_list_lock);
  359. fc_remove_host(shost);
  360. scsi_remove_host(shost);
  361. scsi_host_put(shost);
  362. adapter->scsi_host = NULL;
  363. }
  364. static struct fc_host_statistics*
  365. zfcp_init_fc_host_stats(struct zfcp_adapter *adapter)
  366. {
  367. struct fc_host_statistics *fc_stats;
  368. if (!adapter->fc_stats) {
  369. fc_stats = kmalloc(sizeof(*fc_stats), GFP_KERNEL);
  370. if (!fc_stats)
  371. return NULL;
  372. adapter->fc_stats = fc_stats; /* freed in adapter_release */
  373. }
  374. memset(adapter->fc_stats, 0, sizeof(*adapter->fc_stats));
  375. return adapter->fc_stats;
  376. }
  377. static void zfcp_adjust_fc_host_stats(struct fc_host_statistics *fc_stats,
  378. struct fsf_qtcb_bottom_port *data,
  379. struct fsf_qtcb_bottom_port *old)
  380. {
  381. fc_stats->seconds_since_last_reset =
  382. data->seconds_since_last_reset - old->seconds_since_last_reset;
  383. fc_stats->tx_frames = data->tx_frames - old->tx_frames;
  384. fc_stats->tx_words = data->tx_words - old->tx_words;
  385. fc_stats->rx_frames = data->rx_frames - old->rx_frames;
  386. fc_stats->rx_words = data->rx_words - old->rx_words;
  387. fc_stats->lip_count = data->lip - old->lip;
  388. fc_stats->nos_count = data->nos - old->nos;
  389. fc_stats->error_frames = data->error_frames - old->error_frames;
  390. fc_stats->dumped_frames = data->dumped_frames - old->dumped_frames;
  391. fc_stats->link_failure_count = data->link_failure - old->link_failure;
  392. fc_stats->loss_of_sync_count = data->loss_of_sync - old->loss_of_sync;
  393. fc_stats->loss_of_signal_count =
  394. data->loss_of_signal - old->loss_of_signal;
  395. fc_stats->prim_seq_protocol_err_count =
  396. data->psp_error_counts - old->psp_error_counts;
  397. fc_stats->invalid_tx_word_count =
  398. data->invalid_tx_words - old->invalid_tx_words;
  399. fc_stats->invalid_crc_count = data->invalid_crcs - old->invalid_crcs;
  400. fc_stats->fcp_input_requests =
  401. data->input_requests - old->input_requests;
  402. fc_stats->fcp_output_requests =
  403. data->output_requests - old->output_requests;
  404. fc_stats->fcp_control_requests =
  405. data->control_requests - old->control_requests;
  406. fc_stats->fcp_input_megabytes = data->input_mb - old->input_mb;
  407. fc_stats->fcp_output_megabytes = data->output_mb - old->output_mb;
  408. }
  409. static void zfcp_set_fc_host_stats(struct fc_host_statistics *fc_stats,
  410. struct fsf_qtcb_bottom_port *data)
  411. {
  412. fc_stats->seconds_since_last_reset = data->seconds_since_last_reset;
  413. fc_stats->tx_frames = data->tx_frames;
  414. fc_stats->tx_words = data->tx_words;
  415. fc_stats->rx_frames = data->rx_frames;
  416. fc_stats->rx_words = data->rx_words;
  417. fc_stats->lip_count = data->lip;
  418. fc_stats->nos_count = data->nos;
  419. fc_stats->error_frames = data->error_frames;
  420. fc_stats->dumped_frames = data->dumped_frames;
  421. fc_stats->link_failure_count = data->link_failure;
  422. fc_stats->loss_of_sync_count = data->loss_of_sync;
  423. fc_stats->loss_of_signal_count = data->loss_of_signal;
  424. fc_stats->prim_seq_protocol_err_count = data->psp_error_counts;
  425. fc_stats->invalid_tx_word_count = data->invalid_tx_words;
  426. fc_stats->invalid_crc_count = data->invalid_crcs;
  427. fc_stats->fcp_input_requests = data->input_requests;
  428. fc_stats->fcp_output_requests = data->output_requests;
  429. fc_stats->fcp_control_requests = data->control_requests;
  430. fc_stats->fcp_input_megabytes = data->input_mb;
  431. fc_stats->fcp_output_megabytes = data->output_mb;
  432. }
  433. static struct fc_host_statistics *zfcp_get_fc_host_stats(struct Scsi_Host *host)
  434. {
  435. struct zfcp_adapter *adapter;
  436. struct fc_host_statistics *fc_stats;
  437. struct fsf_qtcb_bottom_port *data;
  438. int ret;
  439. adapter = (struct zfcp_adapter *)host->hostdata[0];
  440. fc_stats = zfcp_init_fc_host_stats(adapter);
  441. if (!fc_stats)
  442. return NULL;
  443. data = kzalloc(sizeof(*data), GFP_KERNEL);
  444. if (!data)
  445. return NULL;
  446. ret = zfcp_fsf_exchange_port_data_sync(adapter->qdio, data);
  447. if (ret) {
  448. kfree(data);
  449. return NULL;
  450. }
  451. if (adapter->stats_reset &&
  452. ((jiffies/HZ - adapter->stats_reset) <
  453. data->seconds_since_last_reset))
  454. zfcp_adjust_fc_host_stats(fc_stats, data,
  455. adapter->stats_reset_data);
  456. else
  457. zfcp_set_fc_host_stats(fc_stats, data);
  458. kfree(data);
  459. return fc_stats;
  460. }
  461. static void zfcp_reset_fc_host_stats(struct Scsi_Host *shost)
  462. {
  463. struct zfcp_adapter *adapter;
  464. struct fsf_qtcb_bottom_port *data;
  465. int ret;
  466. adapter = (struct zfcp_adapter *)shost->hostdata[0];
  467. data = kzalloc(sizeof(*data), GFP_KERNEL);
  468. if (!data)
  469. return;
  470. ret = zfcp_fsf_exchange_port_data_sync(adapter->qdio, data);
  471. if (ret)
  472. kfree(data);
  473. else {
  474. adapter->stats_reset = jiffies/HZ;
  475. kfree(adapter->stats_reset_data);
  476. adapter->stats_reset_data = data; /* finally freed in
  477. adapter_release */
  478. }
  479. }
  480. static void zfcp_get_host_port_state(struct Scsi_Host *shost)
  481. {
  482. struct zfcp_adapter *adapter =
  483. (struct zfcp_adapter *)shost->hostdata[0];
  484. int status = atomic_read(&adapter->status);
  485. if ((status & ZFCP_STATUS_COMMON_RUNNING) &&
  486. !(status & ZFCP_STATUS_ADAPTER_LINK_UNPLUGGED))
  487. fc_host_port_state(shost) = FC_PORTSTATE_ONLINE;
  488. else if (status & ZFCP_STATUS_ADAPTER_LINK_UNPLUGGED)
  489. fc_host_port_state(shost) = FC_PORTSTATE_LINKDOWN;
  490. else if (status & ZFCP_STATUS_COMMON_ERP_FAILED)
  491. fc_host_port_state(shost) = FC_PORTSTATE_ERROR;
  492. else
  493. fc_host_port_state(shost) = FC_PORTSTATE_UNKNOWN;
  494. }
  495. static void zfcp_set_rport_dev_loss_tmo(struct fc_rport *rport, u32 timeout)
  496. {
  497. rport->dev_loss_tmo = timeout;
  498. }
  499. /**
  500. * zfcp_scsi_terminate_rport_io - Terminate all I/O on a rport
  501. * @rport: The FC rport where to teminate I/O
  502. *
  503. * Abort all pending SCSI commands for a port by closing the
  504. * port. Using a reopen avoids a conflict with a shutdown
  505. * overwriting a reopen. The "forced" ensures that a disappeared port
  506. * is not opened again as valid due to the cached plogi data in
  507. * non-NPIV mode.
  508. */
  509. static void zfcp_scsi_terminate_rport_io(struct fc_rport *rport)
  510. {
  511. struct zfcp_port *port;
  512. struct Scsi_Host *shost = rport_to_shost(rport);
  513. struct zfcp_adapter *adapter =
  514. (struct zfcp_adapter *)shost->hostdata[0];
  515. port = zfcp_get_port_by_wwpn(adapter, rport->port_name);
  516. if (port) {
  517. zfcp_erp_port_forced_reopen(port, 0, "sctrpi1");
  518. put_device(&port->dev);
  519. } else {
  520. zfcp_erp_port_forced_no_port_dbf(
  521. "sctrpin", adapter,
  522. rport->port_name /* zfcp_scsi_rport_register */,
  523. rport->port_id /* zfcp_scsi_rport_register */);
  524. }
  525. }
  526. static void zfcp_scsi_rport_register(struct zfcp_port *port)
  527. {
  528. struct fc_rport_identifiers ids;
  529. struct fc_rport *rport;
  530. if (port->rport)
  531. return;
  532. ids.node_name = port->wwnn;
  533. ids.port_name = port->wwpn;
  534. ids.port_id = port->d_id;
  535. ids.roles = FC_RPORT_ROLE_FCP_TARGET;
  536. zfcp_dbf_rec_trig_lock("scpaddy", port->adapter, port, NULL,
  537. ZFCP_PSEUDO_ERP_ACTION_RPORT_ADD,
  538. ZFCP_PSEUDO_ERP_ACTION_RPORT_ADD);
  539. rport = fc_remote_port_add(port->adapter->scsi_host, 0, &ids);
  540. if (!rport) {
  541. dev_err(&port->adapter->ccw_device->dev,
  542. "Registering port 0x%016Lx failed\n",
  543. (unsigned long long)port->wwpn);
  544. return;
  545. }
  546. rport->maxframe_size = port->maxframe_size;
  547. rport->supported_classes = port->supported_classes;
  548. port->rport = rport;
  549. port->starget_id = rport->scsi_target_id;
  550. zfcp_unit_queue_scsi_scan(port);
  551. }
  552. static void zfcp_scsi_rport_block(struct zfcp_port *port)
  553. {
  554. struct fc_rport *rport = port->rport;
  555. if (rport) {
  556. zfcp_dbf_rec_trig_lock("scpdely", port->adapter, port, NULL,
  557. ZFCP_PSEUDO_ERP_ACTION_RPORT_DEL,
  558. ZFCP_PSEUDO_ERP_ACTION_RPORT_DEL);
  559. fc_remote_port_delete(rport);
  560. port->rport = NULL;
  561. }
  562. }
  563. void zfcp_scsi_schedule_rport_register(struct zfcp_port *port)
  564. {
  565. get_device(&port->dev);
  566. port->rport_task = RPORT_ADD;
  567. if (!queue_work(port->adapter->work_queue, &port->rport_work))
  568. put_device(&port->dev);
  569. }
  570. void zfcp_scsi_schedule_rport_block(struct zfcp_port *port)
  571. {
  572. get_device(&port->dev);
  573. port->rport_task = RPORT_DEL;
  574. if (port->rport && queue_work(port->adapter->work_queue,
  575. &port->rport_work))
  576. return;
  577. put_device(&port->dev);
  578. }
  579. void zfcp_scsi_schedule_rports_block(struct zfcp_adapter *adapter)
  580. {
  581. unsigned long flags;
  582. struct zfcp_port *port;
  583. read_lock_irqsave(&adapter->port_list_lock, flags);
  584. list_for_each_entry(port, &adapter->port_list, list)
  585. zfcp_scsi_schedule_rport_block(port);
  586. read_unlock_irqrestore(&adapter->port_list_lock, flags);
  587. }
  588. void zfcp_scsi_rport_work(struct work_struct *work)
  589. {
  590. struct zfcp_port *port = container_of(work, struct zfcp_port,
  591. rport_work);
  592. while (port->rport_task) {
  593. if (port->rport_task == RPORT_ADD) {
  594. port->rport_task = RPORT_NONE;
  595. zfcp_scsi_rport_register(port);
  596. } else {
  597. port->rport_task = RPORT_NONE;
  598. zfcp_scsi_rport_block(port);
  599. }
  600. }
  601. put_device(&port->dev);
  602. }
  603. /**
  604. * zfcp_scsi_set_prot - Configure DIF/DIX support in scsi_host
  605. * @adapter: The adapter where to configure DIF/DIX for the SCSI host
  606. */
  607. void zfcp_scsi_set_prot(struct zfcp_adapter *adapter)
  608. {
  609. unsigned int mask = 0;
  610. unsigned int data_div;
  611. struct Scsi_Host *shost = adapter->scsi_host;
  612. data_div = atomic_read(&adapter->status) &
  613. ZFCP_STATUS_ADAPTER_DATA_DIV_ENABLED;
  614. if (enable_dif &&
  615. adapter->adapter_features & FSF_FEATURE_DIF_PROT_TYPE1)
  616. mask |= SHOST_DIF_TYPE1_PROTECTION;
  617. if (enable_dif && data_div &&
  618. adapter->adapter_features & FSF_FEATURE_DIX_PROT_TCPIP) {
  619. mask |= SHOST_DIX_TYPE1_PROTECTION;
  620. scsi_host_set_guard(shost, SHOST_DIX_GUARD_IP);
  621. shost->sg_prot_tablesize = adapter->qdio->max_sbale_per_req / 2;
  622. shost->sg_tablesize = adapter->qdio->max_sbale_per_req / 2;
  623. shost->max_sectors = shost->sg_tablesize * 8;
  624. }
  625. scsi_host_set_prot(shost, mask);
  626. }
  627. /**
  628. * zfcp_scsi_dif_sense_error - Report DIF/DIX error as driver sense error
  629. * @scmd: The SCSI command to report the error for
  630. * @ascq: The ASCQ to put in the sense buffer
  631. *
  632. * See the error handling in sd_done for the sense codes used here.
  633. * Set DID_SOFT_ERROR to retry the request, if possible.
  634. */
  635. void zfcp_scsi_dif_sense_error(struct scsi_cmnd *scmd, int ascq)
  636. {
  637. scsi_build_sense_buffer(1, scmd->sense_buffer,
  638. ILLEGAL_REQUEST, 0x10, ascq);
  639. set_driver_byte(scmd, DRIVER_SENSE);
  640. scmd->result |= SAM_STAT_CHECK_CONDITION;
  641. set_host_byte(scmd, DID_SOFT_ERROR);
  642. }
  643. struct fc_function_template zfcp_transport_functions = {
  644. .show_starget_port_id = 1,
  645. .show_starget_port_name = 1,
  646. .show_starget_node_name = 1,
  647. .show_rport_supported_classes = 1,
  648. .show_rport_maxframe_size = 1,
  649. .show_rport_dev_loss_tmo = 1,
  650. .show_host_node_name = 1,
  651. .show_host_port_name = 1,
  652. .show_host_permanent_port_name = 1,
  653. .show_host_supported_classes = 1,
  654. .show_host_supported_fc4s = 1,
  655. .show_host_supported_speeds = 1,
  656. .show_host_maxframe_size = 1,
  657. .show_host_serial_number = 1,
  658. .get_fc_host_stats = zfcp_get_fc_host_stats,
  659. .reset_fc_host_stats = zfcp_reset_fc_host_stats,
  660. .set_rport_dev_loss_tmo = zfcp_set_rport_dev_loss_tmo,
  661. .get_host_port_state = zfcp_get_host_port_state,
  662. .terminate_rport_io = zfcp_scsi_terminate_rport_io,
  663. .show_host_port_state = 1,
  664. .show_host_active_fc4s = 1,
  665. .bsg_request = zfcp_fc_exec_bsg_job,
  666. .bsg_timeout = zfcp_fc_timeout_bsg_job,
  667. /* no functions registered for following dynamic attributes but
  668. directly set by LLDD */
  669. .show_host_port_type = 1,
  670. .show_host_symbolic_name = 1,
  671. .show_host_speed = 1,
  672. .show_host_port_id = 1,
  673. .dd_bsg_size = sizeof(struct zfcp_fsf_ct_els),
  674. };