zfcp_scsi.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531
  1. /*
  2. * zfcp device driver
  3. *
  4. * Interface to Linux SCSI midlayer.
  5. *
  6. * Copyright IBM Corporation 2002, 2008
  7. */
  8. #define KMSG_COMPONENT "zfcp"
  9. #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
  10. #include "zfcp_ext.h"
  11. #include <asm/atomic.h>
  12. /* Find start of Sense Information in FCP response unit*/
  13. char *zfcp_get_fcp_sns_info_ptr(struct fcp_rsp_iu *fcp_rsp_iu)
  14. {
  15. char *fcp_sns_info_ptr;
  16. fcp_sns_info_ptr = (unsigned char *) &fcp_rsp_iu[1];
  17. if (fcp_rsp_iu->validity.bits.fcp_rsp_len_valid)
  18. fcp_sns_info_ptr += fcp_rsp_iu->fcp_rsp_len;
  19. return fcp_sns_info_ptr;
  20. }
  21. static void zfcp_scsi_slave_destroy(struct scsi_device *sdpnt)
  22. {
  23. struct zfcp_unit *unit = (struct zfcp_unit *) sdpnt->hostdata;
  24. atomic_clear_mask(ZFCP_STATUS_UNIT_REGISTERED, &unit->status);
  25. unit->device = NULL;
  26. zfcp_erp_unit_failed(unit, 12, NULL);
  27. zfcp_unit_put(unit);
  28. }
  29. static int zfcp_scsi_slave_configure(struct scsi_device *sdp)
  30. {
  31. if (sdp->tagged_supported)
  32. scsi_adjust_queue_depth(sdp, MSG_SIMPLE_TAG, 32);
  33. else
  34. scsi_adjust_queue_depth(sdp, 0, 1);
  35. return 0;
  36. }
  37. static void zfcp_scsi_command_fail(struct scsi_cmnd *scpnt, int result)
  38. {
  39. set_host_byte(scpnt, result);
  40. if ((scpnt->device != NULL) && (scpnt->device->host != NULL))
  41. zfcp_scsi_dbf_event_result("fail", 4,
  42. (struct zfcp_adapter*) scpnt->device->host->hostdata[0],
  43. scpnt, NULL);
  44. /* return directly */
  45. scpnt->scsi_done(scpnt);
  46. }
  47. static int zfcp_scsi_queuecommand(struct scsi_cmnd *scpnt,
  48. void (*done) (struct scsi_cmnd *))
  49. {
  50. struct zfcp_unit *unit;
  51. struct zfcp_adapter *adapter;
  52. int status;
  53. int ret;
  54. /* reset the status for this request */
  55. scpnt->result = 0;
  56. scpnt->host_scribble = NULL;
  57. scpnt->scsi_done = done;
  58. /*
  59. * figure out adapter and target device
  60. * (stored there by zfcp_scsi_slave_alloc)
  61. */
  62. adapter = (struct zfcp_adapter *) scpnt->device->host->hostdata[0];
  63. unit = scpnt->device->hostdata;
  64. BUG_ON(!adapter || (adapter != unit->port->adapter));
  65. BUG_ON(!scpnt->scsi_done);
  66. if (unlikely(!unit)) {
  67. zfcp_scsi_command_fail(scpnt, DID_NO_CONNECT);
  68. return 0;
  69. }
  70. status = atomic_read(&unit->status);
  71. if (unlikely((status & ZFCP_STATUS_COMMON_ERP_FAILED) ||
  72. !(status & ZFCP_STATUS_COMMON_RUNNING))) {
  73. zfcp_scsi_command_fail(scpnt, DID_ERROR);
  74. return 0;;
  75. }
  76. ret = zfcp_fsf_send_fcp_command_task(adapter, unit, scpnt, 0,
  77. ZFCP_REQ_AUTO_CLEANUP);
  78. if (unlikely(ret == -EBUSY))
  79. return SCSI_MLQUEUE_DEVICE_BUSY;
  80. else if (unlikely(ret < 0))
  81. return SCSI_MLQUEUE_HOST_BUSY;
  82. return ret;
  83. }
  84. static struct zfcp_unit *zfcp_unit_lookup(struct zfcp_adapter *adapter,
  85. int channel, unsigned int id,
  86. unsigned int lun)
  87. {
  88. struct zfcp_port *port;
  89. struct zfcp_unit *unit;
  90. int scsi_lun;
  91. list_for_each_entry(port, &adapter->port_list_head, list) {
  92. if (!port->rport || (id != port->rport->scsi_target_id))
  93. continue;
  94. list_for_each_entry(unit, &port->unit_list_head, list) {
  95. scsi_lun = scsilun_to_int(
  96. (struct scsi_lun *)&unit->fcp_lun);
  97. if (lun == scsi_lun)
  98. return unit;
  99. }
  100. }
  101. return NULL;
  102. }
  103. static int zfcp_scsi_slave_alloc(struct scsi_device *sdp)
  104. {
  105. struct zfcp_adapter *adapter;
  106. struct zfcp_unit *unit;
  107. unsigned long flags;
  108. int retval = -ENXIO;
  109. adapter = (struct zfcp_adapter *) sdp->host->hostdata[0];
  110. if (!adapter)
  111. goto out;
  112. read_lock_irqsave(&zfcp_data.config_lock, flags);
  113. unit = zfcp_unit_lookup(adapter, sdp->channel, sdp->id, sdp->lun);
  114. if (unit &&
  115. (atomic_read(&unit->status) & ZFCP_STATUS_UNIT_REGISTERED)) {
  116. sdp->hostdata = unit;
  117. unit->device = sdp;
  118. zfcp_unit_get(unit);
  119. retval = 0;
  120. }
  121. read_unlock_irqrestore(&zfcp_data.config_lock, flags);
  122. out:
  123. return retval;
  124. }
  125. static int zfcp_scsi_eh_abort_handler(struct scsi_cmnd *scpnt)
  126. {
  127. struct Scsi_Host *scsi_host;
  128. struct zfcp_adapter *adapter;
  129. struct zfcp_unit *unit;
  130. struct zfcp_fsf_req *fsf_req;
  131. unsigned long flags;
  132. unsigned long old_req_id = (unsigned long) scpnt->host_scribble;
  133. int retval = SUCCESS;
  134. scsi_host = scpnt->device->host;
  135. adapter = (struct zfcp_adapter *) scsi_host->hostdata[0];
  136. unit = scpnt->device->hostdata;
  137. /* avoid race condition between late normal completion and abort */
  138. write_lock_irqsave(&adapter->abort_lock, flags);
  139. /* Check whether corresponding fsf_req is still pending */
  140. spin_lock(&adapter->req_list_lock);
  141. fsf_req = zfcp_reqlist_find(adapter, old_req_id);
  142. spin_unlock(&adapter->req_list_lock);
  143. if (!fsf_req) {
  144. write_unlock_irqrestore(&adapter->abort_lock, flags);
  145. zfcp_scsi_dbf_event_abort("lte1", adapter, scpnt, NULL, 0);
  146. return retval;
  147. }
  148. fsf_req->data = NULL;
  149. /* don't access old fsf_req after releasing the abort_lock */
  150. write_unlock_irqrestore(&adapter->abort_lock, flags);
  151. fsf_req = zfcp_fsf_abort_fcp_command(old_req_id, adapter, unit, 0);
  152. if (!fsf_req) {
  153. zfcp_scsi_dbf_event_abort("nres", adapter, scpnt, NULL,
  154. old_req_id);
  155. retval = FAILED;
  156. return retval;
  157. }
  158. __wait_event(fsf_req->completion_wq,
  159. fsf_req->status & ZFCP_STATUS_FSFREQ_COMPLETED);
  160. if (fsf_req->status & ZFCP_STATUS_FSFREQ_ABORTSUCCEEDED) {
  161. zfcp_scsi_dbf_event_abort("okay", adapter, scpnt, fsf_req, 0);
  162. } else if (fsf_req->status & ZFCP_STATUS_FSFREQ_ABORTNOTNEEDED) {
  163. zfcp_scsi_dbf_event_abort("lte2", adapter, scpnt, fsf_req, 0);
  164. } else {
  165. zfcp_scsi_dbf_event_abort("fail", adapter, scpnt, fsf_req, 0);
  166. retval = FAILED;
  167. }
  168. zfcp_fsf_req_free(fsf_req);
  169. return retval;
  170. }
  171. static int zfcp_task_mgmt_function(struct zfcp_unit *unit, u8 tm_flags,
  172. struct scsi_cmnd *scpnt)
  173. {
  174. struct zfcp_adapter *adapter = unit->port->adapter;
  175. struct zfcp_fsf_req *fsf_req;
  176. int retval = SUCCESS;
  177. /* issue task management function */
  178. fsf_req = zfcp_fsf_send_fcp_ctm(adapter, unit, tm_flags, 0);
  179. if (!fsf_req) {
  180. zfcp_scsi_dbf_event_devreset("nres", tm_flags, unit, scpnt);
  181. return FAILED;
  182. }
  183. __wait_event(fsf_req->completion_wq,
  184. fsf_req->status & ZFCP_STATUS_FSFREQ_COMPLETED);
  185. /*
  186. * check completion status of task management function
  187. */
  188. if (fsf_req->status & ZFCP_STATUS_FSFREQ_TMFUNCFAILED) {
  189. zfcp_scsi_dbf_event_devreset("fail", tm_flags, unit, scpnt);
  190. retval = FAILED;
  191. } else if (fsf_req->status & ZFCP_STATUS_FSFREQ_TMFUNCNOTSUPP) {
  192. zfcp_scsi_dbf_event_devreset("nsup", tm_flags, unit, scpnt);
  193. retval = FAILED;
  194. } else
  195. zfcp_scsi_dbf_event_devreset("okay", tm_flags, unit, scpnt);
  196. zfcp_fsf_req_free(fsf_req);
  197. return retval;
  198. }
  199. static int zfcp_scsi_eh_device_reset_handler(struct scsi_cmnd *scpnt)
  200. {
  201. struct zfcp_unit *unit = scpnt->device->hostdata;
  202. if (!unit) {
  203. WARN_ON(1);
  204. return SUCCESS;
  205. }
  206. return zfcp_task_mgmt_function(unit, FCP_LOGICAL_UNIT_RESET, scpnt);
  207. }
  208. static int zfcp_scsi_eh_target_reset_handler(struct scsi_cmnd *scpnt)
  209. {
  210. struct zfcp_unit *unit = scpnt->device->hostdata;
  211. if (!unit) {
  212. WARN_ON(1);
  213. return SUCCESS;
  214. }
  215. return zfcp_task_mgmt_function(unit, FCP_TARGET_RESET, scpnt);
  216. }
  217. static int zfcp_scsi_eh_host_reset_handler(struct scsi_cmnd *scpnt)
  218. {
  219. struct zfcp_unit *unit;
  220. struct zfcp_adapter *adapter;
  221. unit = scpnt->device->hostdata;
  222. adapter = unit->port->adapter;
  223. zfcp_erp_adapter_reopen(adapter, 0, 141, scpnt);
  224. zfcp_erp_wait(adapter);
  225. return SUCCESS;
  226. }
  227. int zfcp_adapter_scsi_register(struct zfcp_adapter *adapter)
  228. {
  229. struct ccw_dev_id dev_id;
  230. if (adapter->scsi_host)
  231. return 0;
  232. ccw_device_get_id(adapter->ccw_device, &dev_id);
  233. /* register adapter as SCSI host with mid layer of SCSI stack */
  234. adapter->scsi_host = scsi_host_alloc(&zfcp_data.scsi_host_template,
  235. sizeof (struct zfcp_adapter *));
  236. if (!adapter->scsi_host) {
  237. dev_err(&adapter->ccw_device->dev,
  238. "Registering the FCP device with the "
  239. "SCSI stack failed\n");
  240. return -EIO;
  241. }
  242. /* tell the SCSI stack some characteristics of this adapter */
  243. adapter->scsi_host->max_id = 1;
  244. adapter->scsi_host->max_lun = 1;
  245. adapter->scsi_host->max_channel = 0;
  246. adapter->scsi_host->unique_id = dev_id.devno;
  247. adapter->scsi_host->max_cmd_len = 255;
  248. adapter->scsi_host->transportt = zfcp_data.scsi_transport_template;
  249. adapter->scsi_host->hostdata[0] = (unsigned long) adapter;
  250. if (scsi_add_host(adapter->scsi_host, &adapter->ccw_device->dev)) {
  251. scsi_host_put(adapter->scsi_host);
  252. return -EIO;
  253. }
  254. return 0;
  255. }
  256. void zfcp_adapter_scsi_unregister(struct zfcp_adapter *adapter)
  257. {
  258. struct Scsi_Host *shost;
  259. struct zfcp_port *port;
  260. shost = adapter->scsi_host;
  261. if (!shost)
  262. return;
  263. read_lock_irq(&zfcp_data.config_lock);
  264. list_for_each_entry(port, &adapter->port_list_head, list)
  265. if (port->rport)
  266. port->rport = NULL;
  267. read_unlock_irq(&zfcp_data.config_lock);
  268. fc_remove_host(shost);
  269. scsi_remove_host(shost);
  270. scsi_host_put(shost);
  271. adapter->scsi_host = NULL;
  272. return;
  273. }
  274. static struct fc_host_statistics*
  275. zfcp_init_fc_host_stats(struct zfcp_adapter *adapter)
  276. {
  277. struct fc_host_statistics *fc_stats;
  278. if (!adapter->fc_stats) {
  279. fc_stats = kmalloc(sizeof(*fc_stats), GFP_KERNEL);
  280. if (!fc_stats)
  281. return NULL;
  282. adapter->fc_stats = fc_stats; /* freed in adater_dequeue */
  283. }
  284. memset(adapter->fc_stats, 0, sizeof(*adapter->fc_stats));
  285. return adapter->fc_stats;
  286. }
  287. static void zfcp_adjust_fc_host_stats(struct fc_host_statistics *fc_stats,
  288. struct fsf_qtcb_bottom_port *data,
  289. struct fsf_qtcb_bottom_port *old)
  290. {
  291. fc_stats->seconds_since_last_reset =
  292. data->seconds_since_last_reset - old->seconds_since_last_reset;
  293. fc_stats->tx_frames = data->tx_frames - old->tx_frames;
  294. fc_stats->tx_words = data->tx_words - old->tx_words;
  295. fc_stats->rx_frames = data->rx_frames - old->rx_frames;
  296. fc_stats->rx_words = data->rx_words - old->rx_words;
  297. fc_stats->lip_count = data->lip - old->lip;
  298. fc_stats->nos_count = data->nos - old->nos;
  299. fc_stats->error_frames = data->error_frames - old->error_frames;
  300. fc_stats->dumped_frames = data->dumped_frames - old->dumped_frames;
  301. fc_stats->link_failure_count = data->link_failure - old->link_failure;
  302. fc_stats->loss_of_sync_count = data->loss_of_sync - old->loss_of_sync;
  303. fc_stats->loss_of_signal_count =
  304. data->loss_of_signal - old->loss_of_signal;
  305. fc_stats->prim_seq_protocol_err_count =
  306. data->psp_error_counts - old->psp_error_counts;
  307. fc_stats->invalid_tx_word_count =
  308. data->invalid_tx_words - old->invalid_tx_words;
  309. fc_stats->invalid_crc_count = data->invalid_crcs - old->invalid_crcs;
  310. fc_stats->fcp_input_requests =
  311. data->input_requests - old->input_requests;
  312. fc_stats->fcp_output_requests =
  313. data->output_requests - old->output_requests;
  314. fc_stats->fcp_control_requests =
  315. data->control_requests - old->control_requests;
  316. fc_stats->fcp_input_megabytes = data->input_mb - old->input_mb;
  317. fc_stats->fcp_output_megabytes = data->output_mb - old->output_mb;
  318. }
  319. static void zfcp_set_fc_host_stats(struct fc_host_statistics *fc_stats,
  320. struct fsf_qtcb_bottom_port *data)
  321. {
  322. fc_stats->seconds_since_last_reset = data->seconds_since_last_reset;
  323. fc_stats->tx_frames = data->tx_frames;
  324. fc_stats->tx_words = data->tx_words;
  325. fc_stats->rx_frames = data->rx_frames;
  326. fc_stats->rx_words = data->rx_words;
  327. fc_stats->lip_count = data->lip;
  328. fc_stats->nos_count = data->nos;
  329. fc_stats->error_frames = data->error_frames;
  330. fc_stats->dumped_frames = data->dumped_frames;
  331. fc_stats->link_failure_count = data->link_failure;
  332. fc_stats->loss_of_sync_count = data->loss_of_sync;
  333. fc_stats->loss_of_signal_count = data->loss_of_signal;
  334. fc_stats->prim_seq_protocol_err_count = data->psp_error_counts;
  335. fc_stats->invalid_tx_word_count = data->invalid_tx_words;
  336. fc_stats->invalid_crc_count = data->invalid_crcs;
  337. fc_stats->fcp_input_requests = data->input_requests;
  338. fc_stats->fcp_output_requests = data->output_requests;
  339. fc_stats->fcp_control_requests = data->control_requests;
  340. fc_stats->fcp_input_megabytes = data->input_mb;
  341. fc_stats->fcp_output_megabytes = data->output_mb;
  342. }
  343. static struct fc_host_statistics *zfcp_get_fc_host_stats(struct Scsi_Host *host)
  344. {
  345. struct zfcp_adapter *adapter;
  346. struct fc_host_statistics *fc_stats;
  347. struct fsf_qtcb_bottom_port *data;
  348. int ret;
  349. adapter = (struct zfcp_adapter *)host->hostdata[0];
  350. fc_stats = zfcp_init_fc_host_stats(adapter);
  351. if (!fc_stats)
  352. return NULL;
  353. data = kzalloc(sizeof(*data), GFP_KERNEL);
  354. if (!data)
  355. return NULL;
  356. ret = zfcp_fsf_exchange_port_data_sync(adapter, data);
  357. if (ret) {
  358. kfree(data);
  359. return NULL;
  360. }
  361. if (adapter->stats_reset &&
  362. ((jiffies/HZ - adapter->stats_reset) <
  363. data->seconds_since_last_reset))
  364. zfcp_adjust_fc_host_stats(fc_stats, data,
  365. adapter->stats_reset_data);
  366. else
  367. zfcp_set_fc_host_stats(fc_stats, data);
  368. kfree(data);
  369. return fc_stats;
  370. }
  371. static void zfcp_reset_fc_host_stats(struct Scsi_Host *shost)
  372. {
  373. struct zfcp_adapter *adapter;
  374. struct fsf_qtcb_bottom_port *data;
  375. int ret;
  376. adapter = (struct zfcp_adapter *)shost->hostdata[0];
  377. data = kzalloc(sizeof(*data), GFP_KERNEL);
  378. if (!data)
  379. return;
  380. ret = zfcp_fsf_exchange_port_data_sync(adapter, data);
  381. if (ret)
  382. kfree(data);
  383. else {
  384. adapter->stats_reset = jiffies/HZ;
  385. kfree(adapter->stats_reset_data);
  386. adapter->stats_reset_data = data; /* finally freed in
  387. adapter_dequeue */
  388. }
  389. }
  390. static void zfcp_get_host_port_state(struct Scsi_Host *shost)
  391. {
  392. struct zfcp_adapter *adapter =
  393. (struct zfcp_adapter *)shost->hostdata[0];
  394. int status = atomic_read(&adapter->status);
  395. if ((status & ZFCP_STATUS_COMMON_RUNNING) &&
  396. !(status & ZFCP_STATUS_ADAPTER_LINK_UNPLUGGED))
  397. fc_host_port_state(shost) = FC_PORTSTATE_ONLINE;
  398. else if (status & ZFCP_STATUS_ADAPTER_LINK_UNPLUGGED)
  399. fc_host_port_state(shost) = FC_PORTSTATE_LINKDOWN;
  400. else if (status & ZFCP_STATUS_COMMON_ERP_FAILED)
  401. fc_host_port_state(shost) = FC_PORTSTATE_ERROR;
  402. else
  403. fc_host_port_state(shost) = FC_PORTSTATE_UNKNOWN;
  404. }
  405. static void zfcp_set_rport_dev_loss_tmo(struct fc_rport *rport, u32 timeout)
  406. {
  407. rport->dev_loss_tmo = timeout;
  408. }
  409. struct fc_function_template zfcp_transport_functions = {
  410. .show_starget_port_id = 1,
  411. .show_starget_port_name = 1,
  412. .show_starget_node_name = 1,
  413. .show_rport_supported_classes = 1,
  414. .show_rport_maxframe_size = 1,
  415. .show_rport_dev_loss_tmo = 1,
  416. .show_host_node_name = 1,
  417. .show_host_port_name = 1,
  418. .show_host_permanent_port_name = 1,
  419. .show_host_supported_classes = 1,
  420. .show_host_supported_speeds = 1,
  421. .show_host_maxframe_size = 1,
  422. .show_host_serial_number = 1,
  423. .get_fc_host_stats = zfcp_get_fc_host_stats,
  424. .reset_fc_host_stats = zfcp_reset_fc_host_stats,
  425. .set_rport_dev_loss_tmo = zfcp_set_rport_dev_loss_tmo,
  426. .get_host_port_state = zfcp_get_host_port_state,
  427. .show_host_port_state = 1,
  428. /* no functions registered for following dynamic attributes but
  429. directly set by LLDD */
  430. .show_host_port_type = 1,
  431. .show_host_speed = 1,
  432. .show_host_port_id = 1,
  433. .disable_target_scan = 1,
  434. };
  435. struct zfcp_data zfcp_data = {
  436. .scsi_host_template = {
  437. .name = "zfcp",
  438. .module = THIS_MODULE,
  439. .proc_name = "zfcp",
  440. .slave_alloc = zfcp_scsi_slave_alloc,
  441. .slave_configure = zfcp_scsi_slave_configure,
  442. .slave_destroy = zfcp_scsi_slave_destroy,
  443. .queuecommand = zfcp_scsi_queuecommand,
  444. .eh_abort_handler = zfcp_scsi_eh_abort_handler,
  445. .eh_device_reset_handler = zfcp_scsi_eh_device_reset_handler,
  446. .eh_target_reset_handler = zfcp_scsi_eh_target_reset_handler,
  447. .eh_host_reset_handler = zfcp_scsi_eh_host_reset_handler,
  448. .can_queue = 4096,
  449. .this_id = -1,
  450. .sg_tablesize = ZFCP_MAX_SBALES_PER_REQ,
  451. .cmd_per_lun = 1,
  452. .use_clustering = 1,
  453. .sdev_attrs = zfcp_sysfs_sdev_attrs,
  454. .max_sectors = (ZFCP_MAX_SBALES_PER_REQ * 8),
  455. .shost_attrs = zfcp_sysfs_shost_attrs,
  456. },
  457. };