zfcp_scsi.c 15 KB

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